Page 1 of 2 12 LastLast
Results 1 to 10 of 11

Thread: If Variable is Odd then

  1. #1
    New Member
    Join Date
    Jun 2019
    Posts
    17
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Default If Variable is Odd then

    I am having trouble writing a code to recognize if a number is odd or not and then determining what to do from there... It keeps pooping up with an "object required" error. Please let me know how I can fix it, or if you have ever come across this problem.

    Here is my code

    STNDRD=17

    If STNDRDR Is IsOdd Then
    ActiveCell.Offset(0, 0).Select
    Else
    ActiveCell.Offset(0, -1).Select
    End If


  2. #2
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    51,042
    Post Thanks / Like
    Mentioned
    57 Post(s)
    Tagged
    11 Thread(s)

    Default Re: If Variable is Odd then

    You do not have it structured properly.

    Try this:
    Code:
        If Application.WorksheetFunction.IsOdd(STNRDR) Then
            'Do Nothing
        Else
            ActiveCell.Offset(0, -1).Select
        End If
    There is no point in selecting the ActiveCell. You actually don't even need the comment "Do Nothing" (you can go right to Else). I just put it there as a placeholder.
    Or, you could write it like this:
    Code:
        If Not Application.WorksheetFunction.IsOdd(STNRDR) Then
            ActiveCell.Offset(0, -1).Select
        End If
    Last edited by Joe4; Jun 27th, 2019 at 10:24 AM.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  3. #3
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    29,183
    Post Thanks / Like
    Mentioned
    483 Post(s)
    Tagged
    49 Thread(s)

    Default Re: If Variable is Odd then

    How about
    Code:
       Dim Stndrd As Long
       Stndrd = 16
       
       If Not Application.IsOdd(Stndrd) Then
          ActiveCell.Offset(0, -1).Select
       End If
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  4. #4
    MrExcel MVP FormR's Avatar
    Join Date
    Aug 2011
    Location
    UK
    Posts
    6,111
    Post Thanks / Like
    Mentioned
    25 Post(s)
    Tagged
    2 Thread(s)

    Default Re: If Variable is Odd then

    Or "even"

    Code:
    If Application.IsEven(Stndrd) Then
       ActiveCell.Offset(0, -1).Select
    End If
    [code]your code[/code]

  5. #5
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,317
    Post Thanks / Like
    Mentioned
    93 Post(s)
    Tagged
    33 Thread(s)

    Default Re: If Variable is Odd then

    Quote Originally Posted by FormR View Post
    Or "even"

    Code:
    If Application.IsEven(Stndrd) Then
       ActiveCell.Offset(0, -1).Select
    End If
    Or even...
    Code:
    If Stndrd Like "*[02468]" Then ActiveCell.Offset(0, -1).Select
    or even...
    Code:
    ActiveCell.Offset(0, Stndrd Like "*[02468]").Select
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

  6. #6
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    29,183
    Post Thanks / Like
    Mentioned
    483 Post(s)
    Tagged
    49 Thread(s)

    Default Re: If Variable is Odd then

    Or, yet another way (if dealing with whole numbers)
    Code:
       ActiveCell.Offset(0, -(Stndrd Mod 2)).Select
    Last edited by Fluff; Jun 27th, 2019 at 11:00 AM.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  7. #7
    MrExcel MVP
    Junior Admin
    Joe4's Avatar
    Join Date
    Aug 2002
    Posts
    51,042
    Post Thanks / Like
    Mentioned
    57 Post(s)
    Tagged
    11 Thread(s)

    Default Re: If Variable is Odd then

    Or even...
    Code:
    If Stndrd Like "*[02468]" Then ActiveCell.Offset(0, -1).Select
    or even...
    Code:
    ActiveCell.Offset(0, Stndrd Like "*[02468]").Select
    Just note that may not work properly if the possibility of decimals exists.
    It would incorrectly evaluate to TRUE for a value like 1.6.
    Last edited by Joe4; Jun 27th, 2019 at 11:45 AM.
    TIPS FOR FINDING EXCEL SOLUTIONS
    1. Use the built-in Help that comes with Excel/Access
    2. Use the Search functionality on this board
    3. A lot of VBA code can be acquired by using the Macro Recorder.

    "Give a man a fish, feed him for a day. Teach a man to fish, feed him for life!"

  8. #8
    Board Regular steve the fish's Avatar
    Join Date
    Oct 2009
    Location
    Midlands, UK
    Posts
    7,671
    Post Thanks / Like
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)

    Default Re: If Variable is Odd then

    Quote Originally Posted by Fluff View Post
    Or, yet another way (if dealing with whole numbers)
    Code:
       ActiveCell.Offset(0, -(Stndrd Mod 2)).Select
    That would be this wouldnt it? True being minus 1 for whatever reason.

    Code:
    ActiveCell.Offset(0, Stndrd Mod 2 = 0).Select

  9. #9
    MrExcel MVP
    Moderator
    Fluff's Avatar
    Join Date
    Jun 2014
    Location
    Chippenham
    Posts
    29,183
    Post Thanks / Like
    Mentioned
    483 Post(s)
    Tagged
    49 Thread(s)

    Default Re: If Variable is Odd then

    Yup, that's another way.
    - Posting Data try one of these tools
    - Posting guidelines, forum rules and terms of use
    - Read the FAQs

    Running Office 365 on Win 10

  10. #10
    MrExcel MVP Rick Rothstein's Avatar
    Join Date
    Apr 2011
    Location
    New Jersey, USA
    Posts
    35,317
    Post Thanks / Like
    Mentioned
    93 Post(s)
    Tagged
    33 Thread(s)

    Default Re: If Variable is Odd then

    Quote Originally Posted by Fluff View Post
    Or, yet another way (if dealing with whole numbers)
    Code:
       ActiveCell.Offset(0, -(Stndrd Mod 2)).Select
    Quote Originally Posted by steve the fish View Post
    That would be this wouldnt it? True being minus 1 for whatever reason.

    Code:
    ActiveCell.Offset(0, Stndrd Mod 2 = 0).Select
    Odd and Even only applies to whole numbers. Your tests also work so long as the number fits into a Long data type. If the value is coming from a cell and if that number greater than 2147483647 or less than -2147483648, your codes will generate an Overflow error.
    Last edited by Rick Rothstein; Jun 27th, 2019 at 01:21 PM.
    Rick's "mini" blog... http://www.excelfox.com/forum/f22/
    .
    Want to post a small screen shot? See Part B here.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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