Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, and 2013. 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.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9098) applies to Microsoft Excel 2007, 2010, and 2013. 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

Deleting the Open Document File

Want to delete the document you are currently viewing? Word doesn't provide a way to do it, but you can use the macro in this ...

Discover More

Tools on Developer Tab are Unavailable

Want to add some macros to your workbook? What do you do if you try to add the macros but the program has disabled the tools?

Discover More

Printing a Multi-Line Footer

Add a footer to your document and you may want to make it span more than a single line. This can be easy to do, provided you ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (ribbon)

Copying Named Ranges

Named ranges are a great tool to use in developing formula-heavy workbooks. You may want, at some point, to copy your named ...

Discover More

Specifying a Delimiter when Saving a CSV File in a Macro

You can, within a macro, save a workbook in several different file formats that are understood by Excel. However, you may not ...

Discover More

Adding Buttons to Your Worksheet

You can easily add a button to your worksheet that will allow you to run various macros. This tip shows how easy it is.

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 6 + 0?

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.