Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. 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: Determining If a Number is Odd or Even.
by Allen Wyatt
(last updated December 5, 2016)
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11398) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel 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!
Does your macro need to get some input from a user? Here are the ways that Excel provides for that input to be solicited.Discover More
With a little bit of work, Excel allows you to format individual characters of the text you place in a cell. If you want ...Discover More
When developing a macro, it is often necessary to step through the various code lines so you can see what is happening on ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.