ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Determine if Integer is Odd or Even (https://www.excelbanter.com/excel-programming/347955-determine-if-integer-odd-even.html)

GoFigure[_18_]

Determine if Integer is Odd or Even
 

Excel version: 11 (2003 SP2)
OS: WXP SP2

What's the VBA code equivalent of the Excel IsOdd built-in formula?

It would great if one could use
Application.WorksheetFunction.IsOdd(range), but the IsOdd Excel
built-in is not available in VBA.

Many thanks,

- Al


--
GoFigure
------------------------------------------------------------------------
GoFigure's Profile: http://www.excelforum.com/member.php...fo&userid=4274
View this thread: http://www.excelforum.com/showthread...hreadid=492937


Barb Reinhardt

Determine if Integer is Odd or Even
 
Could you use something related the MOD function?

"GoFigure" wrote in
message ...

Excel version: 11 (2003 SP2)
OS: WXP SP2

What's the VBA code equivalent of the Excel IsOdd built-in formula?

It would great if one could use
Application.WorksheetFunction.IsOdd(range), but the IsOdd Excel
built-in is not available in VBA.

Many thanks,

- Al


--
GoFigure
------------------------------------------------------------------------
GoFigure's Profile:
http://www.excelforum.com/member.php...fo&userid=4274
View this thread: http://www.excelforum.com/showthread...hreadid=492937




Andrew Taylor

Determine if Integer is Odd or Even
 
The IsOdd () function is in the Analysis Toolpack
- see http://snipurl.com/kqja for a recent thread on
how to access these functions in VBA.

As others have said, though, it's very easy to do
it in plain VBA. My personal preference would be
to use the Mod operator:

Function myIsOdd (i as integer) as Boolean
myIsOdd = (i mod 2 = 1)
End function

Andrew Taylor


GoFigure wrote:
Excel version: 11 (2003 SP2)
OS: WXP SP2

What's the VBA code equivalent of the Excel IsOdd built-in formula?

It would great if one could use
Application.WorksheetFunction.IsOdd(range), but the IsOdd Excel
built-in is not available in VBA.

Many thanks,

- Al


--
GoFigure
------------------------------------------------------------------------
GoFigure's Profile: http://www.excelforum.com/member.php...fo&userid=4274
View this thread: http://www.excelforum.com/showthread...hreadid=492937



GoFigure[_19_]

Determine if Integer is Odd or Even
 

Thanks everyone. Using the MOD function works great

--
GoFigur
-----------------------------------------------------------------------
GoFigure's Profile: http://www.excelforum.com/member.php...nfo&userid=427
View this thread: http://www.excelforum.com/showthread.php?threadid=49293


Tom Ogilvy

Determine if Integer is Odd or Even
 
If you will encounter negative numbers, you might want to modify it to

Function myIsOdd(i As Integer) As Boolean
myIsOdd = (Abs(i Mod 2) = 1)
End Function

or use Leith Ross's "AND" approach.

--
Regards,
Tom Ogilvy


"GoFigure" wrote in
message ...

Thanks everyone. Using the MOD function works great.


--
GoFigure
------------------------------------------------------------------------
GoFigure's Profile:

http://www.excelforum.com/member.php...fo&userid=4274
View this thread: http://www.excelforum.com/showthread...hreadid=492937




Bill Martin[_2_]

Determine if Integer is Odd or Even
 
Leith Ross wrote:
Hello GoFigure,

This is a simple test. AND a number with 1 and test the result. It will
be TRUE if odd and FLASE if even.

Here is the UDF:

Function IsOdd(ByRef Rng As Range) As Boolean
IsOdd = Rng.Value AND 1
End Function

Sincerely,
Leith ross


---------------------------

That's kind of a risky thing to do. Excel is not defined to work that way,
though you've plainly found that it does. The risk is that the next upgrade of
Excel may change its internal behavior and make all your old spreadsheets die in
difficult ways to find.

Computer science is full of such "gottchas" over the years.

Good luck...

Bill

Tom Ogilvy

Determine if Integer is Odd or Even
 
VBA is certainly designed to work that way. Where do you get your
information?


From Excel VBA help on AND
=========================
The And operator also performs a bitwise comparison of identically
positioned bits in two numeric expressions and sets the corresponding bit in
result according to the following table:

=========================



So AND, OR, XOR, EQV, IMP, NOT are all bitwise operators.

--

Regards,

Tom Ogilvy





"Bill Martin" wrote in message
...
Leith Ross wrote:
Hello GoFigure,

This is a simple test. AND a number with 1 and test the result. It will
be TRUE if odd and FLASE if even.

Here is the UDF:

Function IsOdd(ByRef Rng As Range) As Boolean
IsOdd = Rng.Value AND 1
End Function

Sincerely,
Leith ross


---------------------------

That's kind of a risky thing to do. Excel is not defined to work that

way,
though you've plainly found that it does. The risk is that the next

upgrade of
Excel may change its internal behavior and make all your old spreadsheets

die in
difficult ways to find.

Computer science is full of such "gottchas" over the years.

Good luck...

Bill




Andrew Taylor

Determine if Integer is Odd or Even
 
Good point - I always forget Mod's ("odd" IMHO) behaviour with
negative numbers. For ultra-terseness (which I don't necessarily
recommend...), you can do

myIsOdd = CBool(i Mod 2)

(or even omit the CBool, as the function returns a Boolean)

ndrew


Tom Ogilvy wrote:
If you will encounter negative numbers, you might want to modify it to

Function myIsOdd(i As Integer) As Boolean
myIsOdd = (Abs(i Mod 2) = 1)
End Function

or use Leith Ross's "AND" approach.

--
Regards,
Tom Ogilvy


"GoFigure" wrote in
message ...

Thanks everyone. Using the MOD function works great.


--
GoFigure
------------------------------------------------------------------------
GoFigure's Profile:

http://www.excelforum.com/member.php...fo&userid=4274
View this thread: http://www.excelforum.com/showthread...hreadid=492937




All times are GMT +1. The time now is 11:36 AM.

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