ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Odd or Even (https://www.excelbanter.com/excel-programming/331851-odd-even.html)

PraxisPete

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


MIKE215

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


Bob Phillips[_7_]

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




JE McGimpsey

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


JE McGimpsey

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


Bob Phillips[_6_]

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




Bob Phillips[_6_]

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




JE McGimpsey

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



JE McGimpsey

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!


Bob Phillips[_7_]

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.



PraxisPete

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!




All times are GMT +1. The time now is 12:29 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com