#1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 32
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,272
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4,624
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,120
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 25
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 06:16 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"