# Macro to dertime Odd/Even number

#### Rocky0201

##### Active Member
Greatings folks...

In column A, starting in row 2 through the end of the column, I have either a null cell or a whole number. I need to determine if this whole number is odd or even. Can anyone help?

Thanks so very much...

#### VoG

##### Legend
Does this help?

Code:
``MsgBox IIf(Range("A2").Value Mod 2 = 0, "Even", "odd")``

#### jimrward

##### Well-known Member
use the MOD function which gives remainder on division

if your number is in A1, then

even =MOD(A1,2)= 0
odd =MOD(A1,2) = 1

=IF(MOD(A1,2) = 0, "EVEN", "ODD")

#### hiker95

##### Well-known Member
Rocky0201,

Before the macro:

Excel Workbook
AB
21
3
410
5
63
7
87
9
101000
Sheet1

After the macro:

Excel Workbook
AB
21odd
3
410even
5
63odd
7
87odd
9
101000even
Sheet1

Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).

1. Copy the below macro, by highlighting the macro code and pressing the keys CTRL + C
3. Press the keys ALT + F11 to open the Visual Basic Editor
4. Press the keys ALT + I to activate the Insert menu
5. Press M to insert a Standard Module
6. Paste the code by pressing the keys CTRL + V
7. Press the keys ALT + Q to exit the Editor, and return to Excel

Code:
``````Sub OddEven()
With Range("B2:B" & Cells(Rows.Count, "A").End(xlUp).Row)
.FormulaR1C1 = "=IF(RC[-1]="""","""",IF(MOD(RC[-1],2)=0,""even"",""odd""))"
.Value = .Value
End With
End Sub``````

Then run the "OddEven" macro.

#### Rocky0201

##### Active Member
Thanks Peter and Jim...

Peter, I used your statement and recevied a compile error--

LR = Sheets(SrcWks).Cells(Rows.Count, 1).End(xlUp).Row
With Range("A1:A" & LR)
IIf(Range("A2").Value Mod 2 = 0, "Even", "odd")
End With

I tried changing the IIf to IF and also received a compile error.

I'm not certain but I may need to convert the value in the cell to numeric first?

Anything you could do would be greatly appreciated...

#### Rocky0201

##### Active Member
My apologies to hiker95 -- I did not realize you had also posted a reply... thanks hiker95!!

#### Richard Schollar

##### MrExcel MVP
If you know they are whole numbers, another way is to And the value with 1:

Code:
``````Dim cell As Range
For Each cell In Range("A2", Cells(Rows.Count,"A").End(xlUp))
If Not IsEmpty(cell.Value) Then cell.Offset(,1) = Iif(cell.Value And 1,""Odd"",""Even"")
Next cell``````

#### Rocky0201

##### Active Member
Thanks Richard...

I am also receiving a compile error in the IF Not statement.

I only need to test for the odd, even or null. I will add code to perfrom various tasks depending on what I find (odd, even or null) in column A.

Thanks again

#### VoG

##### Legend
Try

Code:
``````Sub test2()
Dim LR As Long, i As Long
LR = Range("A" & Rows.Count).End(xlUp).Row
For i = 2 To LR
With Range("A" & i)
Select Case .Value
Case "": .Offset(, 1).Value = "Null"
Case Else: .Offset(, 1).Value = IIf(.Value Mod 2 = 0, "Even", "Odd")
End Select
End With
Next i
End Sub``````

#### Rocky0201

##### Active Member
Thank you very much Peter...<?xml:namespace prefix = o ns = "urn:schemas-microsoft-comfficeffice" /><o></o>
<o></o>
so very close... let me explain what I am trying to accomplish--<o></o>
<o></o>
I have a spreadsheet that, in column A starting at row 2 through a variable number of rows, the following values exist:<o></o>
<o></o>
1) a number from 1 through x or,<o></o>
2) the value "Description:" (plus a description in the row following) or,<o></o>
3) the value "Comment:" (plus the comment in the row following<o></o>
<o></o>
If the value is a number, i need to know if the number is odd or even so that I may do various formatting to the cells in that row (columns A thru H) such as set the fill color to something (for even) and something else (for odd) and set the border to a certain value. I believe that I do not have any issues with knowing what to code for setting fill colors or setting borders.<o></o>
<o></o>
If the value is "Description:", i need to move the value to the B column in the same row and blank ("") the A column in the same row. Additionally, I need to move the row immediately following "Description:" to column B, blank ("") column A in that row and set columns B thru H to merge with wrap text = true in that row.<o></o>
<o></o>
If the value is "Comment:", i need to do the exact same thing as described in the paragraph above.<o></o>
<o></o>
Hope this make sense... if not, please let me know...<o></o>
<o></o>
Thanks again so very much...<o></o>
<o> </o>

1,082,126
Messages
5,363,317
Members
400,725
Latest member
excelingtolearn

### This Week's Hot Topics

• populate from drop list with multiple tables
Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
• Find list of words from sheet2 in sheet1 before a comma and extract text vba
Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
• Dynamic Formula entry - VBA code sought
Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...