How to know if a number is even or odd

chuckchuckit

Well-known Member
Joined
Sep 18, 2010
Messages
541
Need to know if a row is even or odd numbered, such as:
Code:
Dim ROWcursorIsNOW
ROWcursorIsNOW = ActiveCell.Row 'get row our cursor is on
So now need to determine if this row is even or odd, but don't know the math logic to determine this?
Thanks. - Chuck
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You can modify this to do what you need, the main thing is weather there is a remainder when you divde the row you are on by 2 or not.

Code:
Sub test()
If ActiveCell.Row / 2 = 1 Then
MsgBox ("Even")
Else
MsgBox ("Odd")
End If
End Sub
 
Upvote 0
Code:
If (ActiveCell.Row And 1) = 1 Then ' it's odd
 
Upvote 0
Thanks much, that did it. Knew it had to be something to do with the dividing results.

Working with merged cells gets a lot of -- if 200 there then 100 here. ...What is even here might be odd there, etc.

-Chuck
 
Upvote 0
I realized that my code will only work for you on the first two rows, which is what I tested it on. I modified it a bit to work for all rows.

Code:
Sub odd_even()
Field1 = ActiveCell.Row
Result = Field1 / 2
If Result - Int(Result) = 0 Then
MsgBox ("even")
Else
MsgBox ("odd")
End If
End Sub
 
Upvote 0
shg4421's answer seems to have been slightly overlooked - FWIW it's the quickest way to check for an odd/even integer, so it's a very good answer.
 
Upvote 0
I agree. The other example I could not get to work (but may have worked if I experimented with it more).

shg4421's answer does work and is the one I am using. Although if I remember right when I tested it, his example proves even instead of odd. But no matter, as it was the code procedure I could not figure out. And that code does work well. And am glad shg4421 supplied the example.

This board is extremely helpful to those of us that can get in the "stuck loop...".

Thanks.

Chuck
 
Upvote 0

Forum statistics

Threads
1,224,550
Messages
6,179,459
Members
452,915
Latest member
hannnahheileen

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