Solved vba coding for if a value is odd/even

April 22, 2014 at 13:15:56
Specs: Windows 7
Hi Guys,
Am having a bit of trouble with this section of code that I am trying to use

If Sheets("GMPAIRS").Range("J44") = IsOdd Then
    ActiveCell.FormulaR1C1 = "=RANDBETWEEN(1,R[-1]C[6])"
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False

Range.("J44") is an odd value , ie 19. However the coding goes directly to Else without performing the bit of code between.
Am I misunderstanding the use if the 'IsOdd' instruction?
If so, any pointers!

See More: vba coding for if a value is odd/even

Report •

April 22, 2014 at 14:24:21
If Sheets("GMPAIRS").Range("J44") And 1 Then

How To Ask Questions The Smart Way

Report •

April 23, 2014 at 07:47:22
✔ Best Answer
While Razor2.3's elegant suggestion will work based on the inner workings of VBA, I'd like to explain the use of IsOdd since you asked "Am I misunderstanding the use of the 'IsOdd' instruction?"

IsOdd is worksheet function not actually "known" to VBA. In order to use certain Excel based functions, you need to use the WorksheetFunction object:

If WorksheetFunction.IsOdd(Sheets("GMPAIRS").Range("J44")) Then

The way your code is written, VBA is seeing IsOdd as a variable. Since it never gets set to anything, it is "empty" and unless J44 is also empty your If will never be True.

If you were to get used to using Option Explicit, which forces you to define your variables, IsOdd would have been highlighted with the message "Variable not defined" when the VBA editor tried to compile your code. At that point you probably would have realized that you were not using IsOdd correctly.

Click Here Before Posting Data or VBA Code ---> How To Post Data or Code.

message edited by DerbyDad03

Report •

April 23, 2014 at 13:10:23
Thanks for the instruction on this. Still many things I do not know about, just glad that you and the other guys are there to help, tutor and point in the right direction.

Many thanks

Report •

Related Solutions

April 23, 2014 at 16:01:17
I should probably point out the inner working I employed is binary math, so you can use some version of the trick in any language that allows binary AND. Because computers aren't going to abandon base 2 for a long time.

How To Ask Questions The Smart Way

Report •

Ask Question