2.swap Values in Excel VBA - EASY Excel Macros
2.swap Values in Excel VBA - EASY Excel Macros
Values in Excel VBA EASY Excel Macros
Excel Easy
#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
Two values on your worksheet.
Learn more, it's easy
Swap Values
Run Code from a Module
Macro Recorder
Use Relative References
FormulaR1C1
Add a Macro to the Toolbar
Macro Security
Protect Macro
Download Excel File
swapvalues.xls
Place a command button on your worksheet and add the following code lines:
Follow Excel Easy
1. First, we declare a variable called temp of type Double.
Dim temp As Double
2. We initialize the variable temp with the value of cell A1.
temp = Range("A1").Value
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).
Range("A1").Value = Range("B1").Value
4. Finally, we write the value of cell A1 (written to temp) to cell B1.
Range("B1").Value = temp
5. Click the command button two times.
Result:
http://www.exceleasy.com/vba/examples/swapvalues.html 1/2
13/09/2016 Swap Values in Excel VBA EASY Excel Macros
Do you like this free website? Please share this page on Google+
Share 12
2/9 Completed! Learn more about creating macros >
Go to Top: Swap Values | Go to Next Chapter: MsgBox
COPYRIGHT (C) 20102016 WWW.EXCELEASY.COM. ALL RIGHTS RESERVED.
EXCEL 2010 TUTORIAL | HOW TO USE EXCEL | MICROSOFT EXCEL 2010 | VBA IN EXCEL
http://www.exceleasy.com/vba/examples/swapvalues.html 2/2