0% found this document useful (0 votes)
24 views

Chapter 1.2 - Swap Values

This document provides an example of how to swap the values of two cells in Excel using VBA macros. It explains the steps to declare a temporary variable to store the value of one cell, assign the value of the other cell to the first, and then assign the temporary value to the other cell, effectively swapping the values.

Uploaded by

putra.stat11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Chapter 1.2 - Swap Values

This document provides an example of how to swap the values of two cells in Excel using VBA macros. It explains the steps to declare a temporary variable to store the value of one cell, assign the value of the other cell to the first, and then assign the temporary value to the other cell, effectively swapping the values.

Uploaded by

putra.stat11
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Excel Easy

#1 Excel tutorial on the net Excel Introduction Basics Functions Data Analysis VBA 300 Examples Ask us

Swap Values Search Entire Site Go

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


Place a command button on your worksheet and add the following code lines:
FormulaR1C1
1. First, we declare a variable called temp of type Double.

Dim temp As Double Add a Macro to the Toolbar

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
swap­values.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:

2/9 Completed! Learn much more about creating macros >


Back to Top: Swap Values | Go to Next Chapter: MsgBox

You might also like