Chapter 1.2 - Swap Values
Chapter 1.2 - Swap Values
#1 Excel tutorial on the net Excel Introduction Basics Functions Data Analysis VBA 300 Examples Ask us
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. Chapter < >
Situation: Create a Macro
Macro Recorder
2. We initialize the variable temp with the value of cell A1. Macro Security
temp = Range("A1").Value
Protect Macro
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).
Download Excel File
Range("A1").Value = Range("B1").Value
swapvalues.xls
4. Finally, we write the value of cell A1 (written to temp) to cell B1.
Range("B1").Value = temp
Follow Excel Easy
5. Click the command button two times.
Result: