PDA

View Full Version : odd or even



wilg
04-26-2011, 04:30 PM
Can someone help me with trying to build this..

if a1 = odd and a2 <> stop then
run code
else
if a1 = even and a2 <> stop2 then
run code
end if
else
exit sub


I just don't know makes it know odd or even.

Thanks in advance.

Kenneth Hobs
04-26-2011, 04:48 PM
IF a number is divisible by 2 then it is even. e.g.
Msgbox 44 mod 2 = 0,,"44 mod 2 is 0 so True, 44 is an even number."

Paul_Hossler
04-26-2011, 04:52 PM
If Application.WorksheetFunction.IsOdd(A1) Then


Paul

wilg
04-26-2011, 05:07 PM
Hi guys, I was able to manipulate my code for Kens suggestion. Paul, your code seem simpler but it throuws an error. I'm using xl2003 I think I read somewhere that isodd may be an add in?

wilg
04-26-2011, 05:09 PM
oh...I am putting this in the workbook object may this be why?

wilg
04-26-2011, 05:38 PM
This is what I came up with. But it loops after ak119 is even...

If Range("AK119") > 0 And Range("AK119") Mod 2 = 0 And Range("AK121") <> "E" Then
MsgBox "EVEN"
Range("AK121").FormulaR1C1 = "E"
Else
If Range("AK119") > 0 And Range("AK119") Mod 1 = 0 And Range("AK121") <> "O" Then
MsgBox "ODD"
Range("AK121").FormulaR1C1 = "O"
End If
End If

wilg
04-26-2011, 05:42 PM
I ADJUSTED IT TO THIS. SEEMS TO BE OK

If Range("AK119") > 0 And Range("AK119") Mod 2 = 0 And Range("AK121") <> "E" Then
MsgBox "EVEN"
Range("AK121").FormulaR1C1 = "E"
End If
If Range("AK119") > 0 And Range("AK119") Mod 2 = 1 And Range("AK121") <> "O" Then
MsgBox "ODD"
Range("AK121").FormulaR1C1 = "O"
End If

Kenneth Hobs
04-26-2011, 05:45 PM
Mod returns the remainder of a number divided by another. Since all numbers are the same when divided by 1, I don't know why you checked for that.

I don't know how it could loop since you have no loop.

Why not use?
If Range("AK119") > 0 And Range("AK119") Mod 2 = 0 And Range("AK121") <> "E" Then
MsgBox "EVEN"
Range("AK121").FormulaR1C1 = "E"
Else
MsgBox "ODD"
Range("AK121").FormulaR1C1 = "O" End If

Aussiebear
04-26-2011, 06:04 PM
Why are we using Range("AK121").FormulaR1C1? Since its either "E" or "O" as a value, shouldn't it be Range("AK121").Value ?

wilg
04-26-2011, 06:17 PM
Hi guys. Thanks it works well now.

To answer you question Aussiebear I did this to differientate either odd or even to stop the message box from appearing more than once.

Aussiebear
04-26-2011, 07:06 PM
I don't follow your logic here with this code. There are really only two conditional tests here to determine the value of the cell AK121.

If the value of the cell AK119 is both Positive & is Even then cell value in AK121 is "E" if not then "O"

So why not use the following code

If Range("AK119") > 0 And Range("AK119") Mod 2 = 0 Then
MsgBox "EVEN"
Range("AK121").Value = "E"
Else
MsgBox "ODD"
Range("AK121").Value = "O"
End If

macropod
04-27-2011, 01:59 AM
Aside from the message box:
Range("AK121").Value = Chr(69 + ((Range("AK119") Mod 2 + (Range("AK119") = 0) ^ 2) * 10))