+ Reply to Thread
Results 1 to 9 of 9

Determine if Integer is Odd or Even

  1. #1
    Registered User
    Join Date
    12-28-2003
    Posts
    30

    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

  2. #2
    Barb Reinhardt
    Guest

    Re: Determine if Integer is Odd or Even

    Could you use something related the MOD function?

    "GoFigure" <[email protected]> wrote in
    message news:[email protected]...
    >
    > 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. #3
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,258
    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

  4. #4
    Andrew Taylor
    Guest

    Re: 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



  5. #5
    Registered User
    Join Date
    12-28-2003
    Posts
    30

    Re: Determine if Integer is Odd or Even

    Thanks everyone. Using the MOD function works great for what I need. I'll make a note of the AND test, too.

  6. #6
    Tom Ogilvy
    Guest

    Re: 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" <[email protected]> wrote in
    message news:[email protected]...
    >
    > 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
    >




  7. #7
    Bill Martin
    Guest

    Re: 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

  8. #8
    Tom Ogilvy
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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




  9. #9
    Andrew Taylor
    Guest

    Re: 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" <[email protected]> wrote in
    > message news:[email protected]...
    > >
    > > 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 to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1