If Variable is Odd then

ktkelly_1

New Member
Joined
Jun 13, 2019
Messages
17
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

 

Excel Facts

Bring active cell back into view
Start at A1 and select to A9999 while writing a formula, you can't see A1 anymore. Press Ctrl+Backspace to bring active cell into view.
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:
Upvote 0
How about
Code:
   Dim Stndrd As Long
   Stndrd = 16
   
   If Not Application.IsOdd(Stndrd) Then
      ActiveCell.Offset(0, -1).Select
   End If
 
Upvote 0
Or "even" ;)

Code:
If Application.IsEven(Stndrd) Then
   ActiveCell.Offset(0, -1).Select
End If
 
Upvote 0
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
 
Upvote 0
Or, yet another way (if dealing with whole numbers)
Code:
   ActiveCell.Offset(0, -(Stndrd Mod 2)).Select
 
Last edited:
Upvote 0
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:
Upvote 0
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
 
Upvote 0
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:
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,240
Members
452,621
Latest member
Laura_PinksBTHFT

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top