Swap Values in Excel VBA
This example teaches you how to swap two values in Excel VBA. You will often need this structure in more complicated programs as we will see later.
Situation:
Two values on your worksheet.
Place a command button on your worksheet and add the following code lines:
1. First, we declare a variable called temp of type Double.
2. We initialize the variable temp with the value of cell A1.
3. Now we can safely write the value of cell B1 to cell A1 (we have stored the value of cell A1 to temp so we will not lose it).
4. Finally, we write the value of cell A1 (written to temp) to cell B1.
5. Click the command button twice.
Result:
If you're new here, welcome to Excel Easy! Join over 1 million monthly Excel learners. You can find popular courses here: Data Analysis in Excel and Excel VBA.