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

Swapping Two Numbers

by Allen Wyatt
(last updated February 22, 2014)

4

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 (2525) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) 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

Speeding Up Mail Merges

The Mail Merge tool in Word is a great way to create new, customized documents. If you are doing a lot of merging, you may ...

Discover More

Automatically Updating Styles

When you add formatting to some text in your document, Word may apply your formatting to every other part of your document as ...

Discover More

Getting the Proper Type of Ellipses

Type three periods in a row, and the AutoCorrect feature in Word kicks in to exchange that sequence for a special ellipses ...

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 (menu)

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

Running Macros on Hidden Worksheets

Excel allows you to hide worksheets so that they aren't visible to those using your workbook. Hiding worksheets has a ...

Discover More

Testing for an Empty Worksheet

If you are using a macro to process a number of worksheets, you may have a need to know if the worksheet is empty or not. ...

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 nine minus 3?

2015-09-26 10:49:20

Rick Rothstein

Another method without using a helper variable...

A = A Xor B

B = A Xor B

A = A Xor B

Notice the expression to the right of the equal sign is the same for each of the three steps making it easier to remember :-)


2014-02-23 19:11:29

Bill Mallloy

The solution offered by Ioannis works only for numbers, of course. The first tip is simple and straightforward. Most code can afford an extra variable in order to provide some simplicity. KISS.


2014-02-23 07:35:58

Michael (Micky) Avidan

Another interesting approach (without the helper variable:
MyNum1 = MyNum1 & "," & MyNum2
MyNum2 = Split(MyNum1, ",")(0)
MyNum1 = Split(MyNum1, ",")(1)
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2014)
ISRAEL


2014-02-22 13:30:16

Ioannis Nikolopoulos

A solution without the help of another variable:
MyNum1=MyNum1+MyNum2
MyNum2=Mynum1-MyNum2
MyNum1=MyNum1-MyNum2


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon 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.