Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365. 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.
Written by Allen Wyatt (last updated May 2, 2026)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365
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 leaves 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.
If you want to get fancy and do the swapping without using the intermediate TempNum variable, you could use this series of commands:
MyNum1 = MyNum1 Xor MyNum2
MyNum2 = MyNum2 Xor MyNum1
MyNum1 = MyNum1 Xor MyNum2
This approach only works if MyNum1 and MyNum2 are whole numbers, meaning they are Integer or Long data types. (If you need to swap any other type of data, you need to use the previous approach in this tip.) It works because the Xor function basically "encodes" the difference between the values. So, successively applying Xor causes the variables to switch their values. (It is mind exploding to try to understand, but it does work.)
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (9098) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, 2021, 2024, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Swapping Two Numbers.
Program Successfully in Excel! This guide will provide you with all the information you need to automate any task in Excel and save time and effort. Learn how to extend Excel's functionality with VBA to create solutions not possible with the standard features. Includes latest information for Excel 2024 and Microsoft 365. Check out Mastering Excel VBA Programming today!
If you need to find out how many columns are set to be a specific width, you'll need a macro to help determine the info. ...
Discover MoreIf you are getting an error when you try to create an event handler, it could be related to a long-known bug in Excel. ...
Discover MoreWith a little bit of work, Excel allows you to format individual characters of the text you place in a cell. If you want ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2026 Sharon Parq Associates, Inc.
Comments