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: Determining If a Number is Odd or Even.
Written by Allen Wyatt (last updated June 1, 2019)
This tip applies to Excel 97, 2000, 2002, and 2003
A common programming task is validating user input. Often, your macro may need to determine if a number entered by a user is odd or even. For instance, suppose you wrote your own macro that asked the user what worksheet number they wanted to process. If your macro had to process odd and even worksheets differently, then you need to figure out if the number the user provided was odd or even. The technique for this is relatively simple, as shown here:
Even = (UserNum Mod 2) - 1
After execution of this line, Even will be True (-1) if UserNum was even, or False (0) if UserNum was odd.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2273) 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: Determining If a Number is Odd or Even.
Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!
When you use a macro to process data you always run the risk of making that data unusable by Excel. This is especially ...
Discover MoreWhen your macro is processing information in a worksheet, do you need to periodically make the contents of a cell bold? ...
Discover MoreNeed your macro to get some input from a user? The standard way to do this is with the InputBox function, described in ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2019-06-06 01:18:39
Rick Rothstein
Here is another way to test whether a number is even or not...
Even = Num Like "*[02468]"
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.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments