Check if a Value is an Even Number

0 Flares 0 Flares ×

I am currently working on an Excel VBA project that requires me to check if the number obtained by Excel is an even number, or if it is an odd number.

I accomplished this using the Mod operator. The Mod (modulus) operator is the VBA equivalent to the MOD function in Excel. It’s purpose is to find the remainder after a number has been divided by a divisor.

The Mod operator is written as below where a and b  represent variables containing values;

a Mod b

If a stored the value 20 and b stored 6 then the result would be 2. And if a stored 20 and b stored 4 then the result would be 0.

To check if a value is an even or an odd number I used the Mod operator to divide the variable holding the number by 2. If the result returned 0 then it must be an even number. And if not then it must be an odd.

The end result was to use an If… Then… Else conditional statement to take action on the answer from the Mod operator. This looked like the below;

If a Mod 2 = 0 then

The number is even, do something


The number is odd, do something else

End If

2 thoughts on “Check if a Value is an Even Number

Leave a Reply

Your email address will not be published. Required fields are marked *