Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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
Posted to microsoft.public.excel.programming
|
|||
|
|||
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 |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
How do I add an integer to an existing integer? | Excel Worksheet Functions | |||
integer | Excel Worksheet Functions | |||
What function to determine the second small integer from a list? | Excel Worksheet Functions | |||
What function to determine the second small integer from a list? | Excel Discussion (Misc queries) | |||
Get next Integer value | Excel Programming |