even

  • Thread starter Thread starter ranswert
  • Start date Start date
Use the MOD function

myvariable = 9
If (myvariable Mod 2) > 0 Then
MsgBox "Odd"
Else
MsgBox "Even"
End If

Mike
 
Thanks I'll give that a try.

Mike H said:
Use the MOD function

myvariable = 9
If (myvariable Mod 2) > 0 Then
MsgBox "Odd"
Else
MsgBox "Even"
End If

Mike
 
How do I find out if a variable is even?

Just make sure you are working with Integers since vba's MOD rounds the
input.

Sub xxx()
Dim n
n = 9.5

'Considered Even
If (n Mod 2) > 0 Then
MsgBox "Odd"
Else
MsgBox "Even"
End If

'Odd
MsgBox WorksheetFunction.IsOdd(9.999999999)
'Even
MsgBox WorksheetFunction.IsOdd(9.9999999999)
End Sub
 
You can also do it this way (execution should be marginally faster)...

MyVariable = 9
If MyVariable And 1 Then
MsgBox "Odd"
Else
MsgBox "Even"
End If

Rick
 

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

Back
Top