Macro to dertime Odd/Even number

Rocky0201

Active Member
Joined
Aug 20, 2009
Messages
278
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...
 

Excel Facts

Select all contiguous cells
Pressing Ctrl+* (asterisk) will select the "current region" - all contiguous cells in all directions.
Does this help?

Code:
MsgBox IIf(Range("A2").Value Mod 2 = 0, "Even", "odd")
 
Upvote 0
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")
 
Upvote 0
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).

Adding the Macro
1. Copy the below macro, by highlighting the macro code and pressing the keys CTRL + C
2. Open your workbook
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.
 
Upvote 0
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...
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
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
 
Upvote 0
Thank you very much Peter...<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p></o:p>
so very close... let me explain what I am trying to accomplish--<o:p></o:p>
<o:p></o:p>
I have a spreadsheet that, in column A starting at row 2 through a variable number of rows, the following values exist:<o:p></o:p>
<o:p></o:p>
1) a number from 1 through x or,<o:p></o:p>
2) the value "Description:" (plus a description in the row following) or,<o:p></o:p>
3) the value "Comment:" (plus the comment in the row following<o:p></o:p>
<o:p></o:p>
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:p></o:p>
<o:p></o:p>
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:p></o:p>
<o:p></o:p>
If the value is "Comment:", i need to do the exact same thing as described in the paragraph above.<o:p></o:p>
<o:p></o:p>
Hope this make sense... if not, please let me know...<o:p></o:p>
<o:p></o:p>
Thanks again so very much...<o:p></o:p>
<o:p> </o:p>
 
Upvote 0

Forum statistics

Threads
1,214,391
Messages
6,119,249
Members
448,879
Latest member
oksanana

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