Written by Allen Wyatt (last updated September 2, 2021)
This tip applies to Word 2007, 2010, 2013, and 2016
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 us 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:
WordTips is your source for cost-effective Microsoft Word training. (Microsoft Word is the most popular word processing software in the world.) This tip (11999) applies to Microsoft Word 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Word here: Swapping Two Numbers.
Do More in Less Time! Are you ready to harness the full power of Word 2013 to create professional documents? In this comprehensive guide you'll learn the skills and techniques for efficiently building the documents you need for your professional and your personal life. Check out Word 2013 In Depth today!
You can easily store your macros in a template. If you create a document based on the template and then the document is ...
Discover MoreOne of the math functions you can use in your macros is the Int function. It provides a way for you to derive an integer ...
Discover MoreWhen creating macros, you might want to know if a user has made changes in the document or not. Here's how to figure that ...
Discover MoreFREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
2016-05-02 22:27:44
Bernard Martin
Long ago when memory was a scarce and expensive ressource, I used to swap two numbers without using a temporary variable, as follows :
MyNum1 = MyNum1 Xor MyNum2
MyNum2 = MyNum2 Xor MyNum1
MyNum1 = MyNum1 Xor MyNum2
You can check that it actually works!
Got a version of Word that uses the ribbon interface (Word 2007 or later)? This site is for you! If you use an earlier version of Word, visit our WordTips site focusing on the menu interface.
Visit the WordTips channel on YouTube
FREE SERVICE: Get tips like this every week in WordTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2023 Sharon Parq Associates, Inc.
Comments