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.

Determining If a Number is Odd or Even

Written by Allen Wyatt (last updated June 17, 2020)
This tip applies to Excel 2007, 2010, 2013, and 2016


16

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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Periodically Delete TMP Files

After using Word for a while, you may notice some "litter" of unused files on your hard drive. This tip explains how ...

Discover More

Transposing Two Characters

If you have two characters in the wrong order, you might be interested in a shortcut you can use to switch their order. ...

Discover More

Two Printed Copies to Different Paper Trays

Many modern printers include multiple paper trays that can be used for different types or colors of paper. Word allows ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Selecting a Range of Cells Relative to the Current Cell

When processing information in a macro, you often need to select different cells relative to the currently selected ...

Discover More

Ctrl+Break Won't Work to Stop a Macro

When you need to stop a macro while it is running, you normally press Ctrl+Break. What are you to do if the keypress ...

Discover More

Creating a String in a Macro

Need to put together a bunch of characters to create a text string? You can do it in your macros by using the String ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 8 + 7?

2019-06-01 16:08:59

Rick Rothstein

Another way to test whether a number is even or not...

Num Like "*[02468]"


2015-05-26 14:11:53

Aldo

OOPS ... made a typo ...

It should be this:

A / 2 = Int(A / 2) ... in 1.83203125 sec


2015-05-26 14:09:45

Aldo

Here are some times I checked on my i7 desktop with Excel 2013 VBA ...

A = Int(A / 2) ... in 1.615234375 sec
(A Mod 2) = 0 ... in 1.0390625 sec
(A Mod 2) - 1 ... in 1.2734375 sec
Application.IsOdd(A) ... in 275.92578125 sec
WorksheetFunction.IsEven(A) ... in 96.341796875 sec

These time were generated on a 100,000,000 For...Next loop.

Clearly (A Mod 2) = 0 is the fastest; over 20% faster than the method shown here. At the end of the day, most macro/VBA "normal" use wouldn't really notice any difference in performance using any even/odd check method above. But if you are a programmer/developer who wants to optimize your code, consider native commands as often as possible in VBA.


2015-05-25 05:46:36

balthamossa2b

Well, I stand corrected.


2015-05-24 23:32:44

Locke Garmin

Ok, I couldn't help myself. :) I did the same tests only this time 100,000,000 times and got the results on the following methods:

(i Mod 2) - 1
1.70 Seconds

(i Mod 2) = 0
1.52 Seconds

Hardly significant savings but interesting! :)


2015-05-24 23:22:17

LockeGarmin

@balthamossa2b

I got interested in the idea of the worksheet functions being slower that I did a couple of tests. I ran a for loop 1,000,000 on each method like the following:

Dim b As Boolean
Dim i As Long: i = 2
dim x as Long

'Start Stopwatch Macro
For x = 1 To 1000000
b = (i Mod 2) - 1
Next x
'End Stopwatch Macro

And came to the following results:

(i Mod 2) - 1
0.02 Seconds

Excel.WorksheetFunction.IsEven(i)
2.19 Seconds

Application.WorksheetFunction.IsEven(i)
2.27 Seconds

Application.IsEven(i)
5.80 Seconds

So I concluded that there is a small time penalty you would start running into if you start testing quite a few numbers for being even or odd. I hope there is another VBA nut who finds this as interesting as I did! :)


2015-05-23 05:49:36

balthamossa2b

@Allen

I don't think you get a time penalty for using a WorksheetFunction, it only happens if you go get a value from the book. Which is once in both examples.

Furthermore, native functions tend to be faster than custom made.


2015-05-22 19:03:10

Michael (Micky) Avidan

@Allan,
To my opinion the average Excelist isn't familiar with the results -1 and 0.
He wants to get a straight answer like TRUE or FALSE.
Therefore my suggestion would be one of the following (Short) commands:
'-------------------
Sub Is_Odd()
MsgBox Application.IsOdd([A1])
MsgBox CBool([A1] Mod 2)
End Sub
'--------
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL


2015-05-22 12:26:29

awyatt

Yep, you can use the WorksheetFunction method, as Eric said. Compare these two lines, however:

x = Application.WorksheetFunction.ISEVEN(Range("A1").Value)
x = (Range("A1").Value Mod 2) - 1

As a matter of preference, the second is better. It is not only shorter, but it doesn't have the time penalty of using the WorksheetFunction method. The MOD operator doesn't make it it "so complicated" (as Eric stated); it actually makes it less complicated and faster.

-Allen


2015-05-22 11:03:14

Eric Augusta

Why are we making this so complicated by using MOD? You can use the standard functions ISEVEN or ISODD in both situations. In a workbook just use ISEVEN or ISODD directly to return either TRUE or FALSE. In VBA you can use the same functions by writing code like this:
Dim x as Boolean
x = Application.WorksheetFunction.ISEVEN(Range("A1").Value)


2015-05-22 10:22:26

awyatt

icy322: Absolutely nothing, if you are working in a workbook. Those functions won't work directly in a macro, though.

And, as is stated in the first two sentence of this tip, this is for use in a macro.

-Allen


2015-05-22 10:20:26

icy322

What's wrong with using the iseven and isodd funtion?=iseven(a1) or = isodd(a1)


2015-05-22 10:16:42

awyatt

Gary: This tip is for use in a macro, in VBA. Thus, no cell reference is required. In the example in the tip, the value being evaluated is in the UserNum variable.

-Allen


2015-05-22 10:05:08

Gary Lundblad

Forgive my ignorance, but I'm not following how this formula would work, as there is no cell reference in the formula. How does it know what cell you want evaluated? I'm probably missing something really obvious.

Thank you!

Gary Lundblad


2015-05-22 07:30:18

balthamossa2b

From personal experience and Euler Project tinkering, mod does not work with numbers longer than Long's limit (2e9).

In that case the workaround would be to test the last digit of the number. Something like:

Val(Right(X,1)) Mod 2

...Supposing the number is an integer. ANd seeing how CInt and CLng don't work with big numbers, you are stuck.

Two solutions:

- Treat the number as text. Remember that Excel has a native limitation of 15 digits per number, so if your number is bigger than 1e15 it won't work.

- Use an addon specialized in big numbers. My favourite is xNum:http://www.bowdoin.edu/~rdelevie/excellaneous/


2012-02-25 14:38:43

Ken Kast

This solution depends on knowing that -1 will be treated as True. An implementation-independent solution is Even = (UserNum Mod 2) = 0. The other advantage to this is that it is literally the definition of even. In a spreadsheet it would be =Mod(UserNum, 2) = 0.


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.