Solved

# Odd or even?

Posted on 1999-12-22
Medium Priority
767 Views
Hi,

Does anyone know how to check in VBA whether an integer is odd or even?
In Excel the methods IsOdd and IsEven are available, but they don't seem to be around in VBA. Not even with the worksheetfunctions object.

Grtx, Eric
0
Question by:ehout
• 2
• 2

LVL 23

Accepted Solution

ahammar earned 150 total points
ID: 2300317
You can use the Mod function:
Ie..to check the integer in cell A1,
try this:

Dim YourNumber As Integer
YourNumber = Range("A1").Value

If YourNumber Mod 2 = 0 Then
MsgBox YourNumber & " is even"
Else
MsgBox YourNumber & " is odd"
End If

Cheers!
ahammar
0

LVL 23

Expert Comment

ID: 2300324
For a little more explanation (of what I know about it anyway):

The mod function actually returns the remainder of a division problem, so if you divide a number by 2, and the remainder is 0, then it must be even.

Cheers!
ahammar
0

Expert Comment

ID: 2300350
Bit of a novice programmer but one way you could do it is using the MOD operator.
This divides 'number1' by 'number2' and gives you the remainder so if you divide your integer by 2 and the remainder is greater than 0 then the integer is odd.

e.g.
[result field] = 10 mod 2
(The [result field] will be 0 therefore 10 is an even number)

e.g. 2
[result field] = 11 mod 2
([result field] will be 1 therefore 11 is an odd number)

You may find that a more experienced programmer can offer an easier solution but see if that helps in the meantime.
0

Expert Comment

ID: 2300354
Ooops - beaten to it.

Shouldn't have taken so long submitting.
0

LVL 3

Author Comment

ID: 2300369
Hmmm... Should have thougth about it myself. Though not exactly what I was looking for, indeed it is a way that suits my needs. Stupid of me not to think of this function.

However, to be fair: you were rigth, so enjoy your new points.

Grtx, Eric
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is about creating a new Microsoft Online User Profile account along with how to transfer your files and settings. You may be faced with this situation if your existing user profile has become corrupted.
Manually copying shapes and their assigned macros one by one to a new location can be tedious, but if you use the Excel utility workbook attached to this article, the process will be much quicker and easier.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
###### Suggested Courses
Course of the Month7 days, 2 hours left to enroll