even numbers

G

GB

Tommi said:
How to check in VBA code if number is even (or odd)?
Think of a number, divide it by 2, take the integer part, and multiply that
by 2. Then take away the number you first thought of. If you are left with
nothing then the number you started with was even.

HTH
Geoff
 
R

Ron de Bruin

Try this Tommi

Sub test()
If ActiveCell.Value Mod 2 = 0 Then
MsgBox "Even"
Else
MsgBox "odd"
End If
End Sub
 
K

Kevin

Use the Mod operator.

dim EvenOdd as string

if(x mod y =0)then
EvenOdd="Even"
else
EvenOdd="Odd"
End If

The mod operator divides two numbers and returns the
remainder. If the remainder is 0 it is even.

Hope that helps!

Kevin
 
D

Dana DeLouis

Just another option. If you have a vba library reference set to
atpvbaen.xls, then you can use IsOdd / IsEven directly.

Sub Demo()
Debug.Print IsOdd(12345678901234)
Debug.Print IsEven(12345678901234)

Debug.Print IsOdd(12345678901235)
Debug.Print IsEven(12345678901235)
End Sub

returns...
False
True
True
False
 
T

Tommi

I tried this. I set the Tools/Referencese to atpvbaen.xls and then used
IsEven. but I get this "Run-Time Error '1004' The macro " cannot be found"
What is wrong?
 
D

Dana DeLouis

I'm not sure. If I don't have the reference set, then I get a
"Compile error: Sub or Function not defined"

I'm just guessing. Unselect this in the vba library. Go to the worksheet,
and select <Tools> <Add-Ins> and make sure the Analysis ToolPak is selected.
Then, try the library reference again.
 
T

Tommi

Thanks for your mail Dana!
This is what I did, and now it works:
I checked Tools/Add-Ins/Analysis Toolpak - VBA in Excel.
Then I checked Tools/References/atpvbaen.xls in Visual Basic Editor.
(for you with similar problem: if you don't see atpvbaen.xls in Visual Basis
Editor Tools/References,
click Browse and find file atpvbaen.xla - probably in \Microsoft Office\
Office10\Library, or something like that)

Regards,
Tommi
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top