If Variable is Odd then

ktkelly_1

New Member
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

 

Joe4

MrExcel MVP, Junior Admin
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:

Fluff

MrExcel MVP, Moderator
How about
Code:
   Dim Stndrd As Long
   Stndrd = 16
   
   If Not Application.IsOdd(Stndrd) Then
      ActiveCell.Offset(0, -1).Select
   End If
 

Rick Rothstein

MrExcel MVP
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
 

Fluff

MrExcel MVP, Moderator
Or, yet another way (if dealing with whole numbers)
Code:
   ActiveCell.Offset(0, -(Stndrd Mod 2)).Select
 
Last edited:

Joe4

MrExcel MVP, Junior Admin
Or even...
Code:
If Stndrd Like "*[02468]" Then ActiveCell.Offset(0, -1).Select[/COLOR]
or even...
Code:
[COLOR=#333333]ActiveCell.Offset(0, Stndrd Like "*[02468]").Select[/COLOR]

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:

Rick Rothstein

MrExcel MVP
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
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:

Some videos you may like

This Week's Hot Topics

  • Importing multiple excel files into one spreadsheet
    Hi, I'm trying to import multiple excel files (with the same format into a single spreadsheet) so that each day's file is listed underneath the...
  • find many based on a certain criteria
    good evening, I hope someone can help me? I have a workbook sheet 2 contains lots of data.... I would like to be able to find anything on sheet...
  • How to copy multiple rows using If
    Hi all, I'm very new to VBA and have written this simple code to copy certain cells if a certain cell within that row contains any data. I need...
  • VBA If statement
    Dear All, I have two dates, where I'd like a message box to pop, if the dates are between this criteria. [CODE] sDate1 = #10/1/2019#...
  • Text Format
    I have a sheet for user to keyin the data. The format of the data can be 451 / 1903, 0012 / 9908 or 00287 / 0099. The number after the "/" is...
  • Syntax errors
    Good Morning, Trying to compile a workbook, I keep getting a few errors. Here are the first two: [code=rich]Syntax Error: Function...
Top