Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 54
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default 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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1
Default 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

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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





  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 105
Default 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
  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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



  #8   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default 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


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


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I add an integer to an existing integer? Aaron Excel Worksheet Functions 3 December 17th 09 09:46 PM
integer integer format Excel Worksheet Functions 1 May 3rd 07 06:45 PM
What function to determine the second small integer from a list? Eric Excel Worksheet Functions 3 December 30th 05 11:56 PM
What function to determine the second small integer from a list? Eric Excel Discussion (Misc queries) 1 December 30th 05 02:05 PM
Get next Integer value Stuart[_5_] Excel Programming 7 February 16th 04 04:48 AM


All times are GMT +1. The time now is 10:34 PM.

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"