Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Swapping Two Numbers.

Swapping Two Numbers

by Allen Wyatt
(last updated October 5, 2016)

3

If you do any serious macro programming, there will eventually come a time when you want to swap the values in two numeric variables. In some versions of BASIC, there are commands that handle this. VBA leave you to our own devices, however. The following technique should do the trick for most people:

    TempNum = MyNum1
    MyNum1 = MyNum2
    MyNum2 = TempNum

When completed, the values in MyNum1 and MyNum2 have been swapped, and TempNum doesn't matter since it was intended (by this technique) as a temporary variable anyway.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9098) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Swapping Two Numbers.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He  is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Turning Off Sharing

All good things must come to an end at some point. When you are done sharing your workbook with others, this is how you ...

Discover More

Downloading Documents to Your Local Computer

Want to pull a document out of the cloud and store it on your local hard drive? Google Drive provides the ability to get ...

Discover More

Enhanced Filling

Using the AutoFill feature of Excel is very handy. If you want to expand the utility offered by the feature, all you need ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Skipping Hidden Rows in a Macro

As your macro processes information in a worksheet, you may want to make sure that it skips over rows that are hidden. ...

Discover More

Automatically Enabling Macros for Specific Workbooks

On your system you may have workbooks that contain macros you know are safe to use. Microsoft provides two things you can ...

Discover More

Easily Changing the Default Drive and Directory

Need a quick way to change the default drive and directory in a macro you are writing? Here's the commands to do it and a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is five more than 8?

2013-10-27 12:21:19

jorge

Of course, why do things easily if they can go by the hard way??


2013-10-20 05:53:00

Michael (Micky) Avidan


Here is another approach (a pure gimmick) to swap two variables without the TempNum:

Sub Swap_2_Variables_Without_TempNum()
Var1 = 2
Var2 = 4
Var1 = Var1 & " " & Var2
Var2 = Split(Var1, " ")(0)
Var1 = Split(Var1, " ")(1)
MsgBox "Varianle Var1 was swapped to: " & Var1
MsgBox "Varianle Var2 was swapped to: " & Var2
End Sub

Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL


2013-10-19 05:26:34

Chris

Rather obvious I thought!


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.