Odd or even?

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
LVL 3
ehoutAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ahammarCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ahammarCommented:
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
TonyWCommented:
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
TonyWCommented:
Ooops - beaten to it.

Shouldn't have taken so long submitting.
0
ehoutAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Office

From novice to tech pro — start learning today.