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.
Solve Real Business Problems Master business modeling and analysis techniques with Excel and transform data into bottom-line results. This hands-on, scenario-focused guide shows you how to use the latest Excel tools to integrate data from multiple tables. Check out Microsoft Excel 2013 Data Analysis and Business Modeling today!
When reading information from a text file, your macro may need to start reading at a place other than the beginning of ...
Discover MoreEver want to know how many cells in a worksheet (or a selection) are shaded in some way? You can create a handy little ...
Discover MoreIf you've got a list of potential words, and you want to know which of those potential words are real, you'll appreciate ...
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