Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
Hi Everybody
I think this should be straight forward but brain is just not working today How can I use VBA code to determine if the contents of a varible is odd or even? Many thanks |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
Hi PraxisPete
You can use the MOD operator to return the remainder of a division by 2 VAR1 = 13 ANS = VAR1 MOD 2 and test the value of ANS. ANS = 1 and is odd in this case. Regards, Mike "PraxisPete" wrote: Hi Everybody I think this should be straight forward but brain is just not working today How can I use VBA code to determine if the contents of a varible is odd or even? Many thanks |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
Public Function IsOdd(Val) As Boolean
IsOdd = (Val \ 2) * 2 < Val End Function -- HTH Bob Phillips "MIKE215" wrote in message ... Hi PraxisPete You can use the MOD operator to return the remainder of a division by 2 VAR1 = 13 ANS = VAR1 MOD 2 and test the value of ANS. ANS = 1 and is odd in this case. Regards, Mike "PraxisPete" wrote: Hi Everybody I think this should be straight forward but brain is just not working today How can I use VBA code to determine if the contents of a varible is odd or even? Many thanks |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
One way:
Dim bEven As Boolean bEven = variable Mod 2 = 0 Dim bOdd As Boolean bOdd = variable Mod 2 = 1 In article , "PraxisPete" wrote: Hi Everybody I think this should be straight forward but brain is just not working today How can I use VBA code to determine if the contents of a varible is odd or even? Many thanks |
#5
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
Note that this returns true for non-integer values (which are neither
odd nor even). OTOH, the ATP's ISODD function truncates the number first, so =ISODD(1.9) === FALSE =ISODD(2.9) === TRUE which seems worse to me. In article , "Bob Phillips" wrote: Public Function IsOdd(Val) As Boolean IsOdd = (Val \ 2) * 2 < Val End Function |
#6
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
Strange, 1.3 MOD 2 also returns 1 which the previous poster called as Odd,
but you didn't seek to point that out? And if non-integer values need to be catered for it is easily extended Public Function IsOdd(Val) If Val \ 1 < Val Then IsOdd = "Invalid value" Else IsOdd = (Val \ 2) * 2 < Val End If End Function Bob "JE McGimpsey" wrote in message ... Note that this returns true for non-integer values (which are neither odd nor even). OTOH, the ATP's ISODD function truncates the number first, so =ISODD(1.9) === FALSE =ISODD(2.9) === TRUE which seems worse to me. In article , "Bob Phillips" wrote: Public Function IsOdd(Val) As Boolean IsOdd = (Val \ 2) * 2 < Val End Function |
#7
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
And this returns bEven as true for 1.9, but False for 2.9, and vice versa
for bOdd! Bob "JE McGimpsey" wrote in message ... One way: Dim bEven As Boolean bEven = variable Mod 2 = 0 Dim bOdd As Boolean bOdd = variable Mod 2 = 1 In article , "PraxisPete" wrote: Hi Everybody I think this should be straight forward but brain is just not working today How can I use VBA code to determine if the contents of a varible is odd or even? Many thanks |
#8
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
I didn't even evaluate the previous poster's method - I guess I assumed
that since it was in all caps, it was a worksheet solution, and you were correcting it. No offense meant! The solution I provided in a different subthread assumed that "variable" was of the appropriate type, which I should have pointed out. I think that the fix to your function would depend on the situation - rather than "Invalid value", which necessitates the function return a variant, it might be more appropriate to keep the boolean function and return False. One could even explicitly type Val as a Long, and perform the implicit coercion in which a single or double would be rounded. In article , "Bob Phillips" wrote: Strange, 1.3 MOD 2 also returns 1 which the previous poster called as Odd, but you didn't seek to point that out? And if non-integer values need to be catered for it is easily extended Public Function IsOdd(Val) If Val \ 1 < Val Then IsOdd = "Invalid value" Else IsOdd = (Val \ 2) * 2 < Val End If End Function |
#9
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
Yup - as I said in the other subthread, I wasn't assuming this was being
used in a function with an untyped argument, so I further assumed that "variable" was of the appropriate type (e.g., Long or Integer). But I should have stated that, of course. Thanks for the correction! In article , "Bob Phillips" wrote: And this returns bEven as true for 1.9, but False for 2.9, and vice versa for bOdd! |
#10
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
"JE McGimpsey" wrote in message ... I think that the fix to your function would depend on the situation - rather than "Invalid value", which necessitates the function return a variant, it might be more appropriate to keep the boolean function and return False. One could even explicitly type Val as a Long, and perform the implicit coercion in which a single or double would be rounded. Exactly, without knowing the full requirement we are just fishing in the dark. |
#11
Posted to microsoft.public.excel.programming
|
|||
|
|||
Odd or Even
Thanks for the respones, I just can think today.
"JE McGimpsey" wrote: Yup - as I said in the other subthread, I wasn't assuming this was being used in a function with an untyped argument, so I further assumed that "variable" was of the appropriate type (e.g., Long or Integer). But I should have stated that, of course. Thanks for the correction! In article , "Bob Phillips" wrote: And this returns bEven as true for 1.9, but False for 2.9, and vice versa for bOdd! |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|