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...
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,651
Does this help?

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

jimrward

Well-known Member
Joined
Feb 24, 2003
Messages
1,708
Office Version
2016, 2013, 2011, 2010, 2007
Platform
Windows
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
Joined
Apr 8, 2009
Messages
17,647
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.
 

Rocky0201

Active Member
Joined
Aug 20, 2009
Messages
278
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
Joined
Aug 20, 2009
Messages
278
My apologies to hiker95 -- I did not realize you had also posted a reply... thanks hiker95!!
 

Richard Schollar

MrExcel MVP
Joined
Apr 19, 2005
Messages
23,707
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
Joined
Aug 20, 2009
Messages
278
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
Joined
Jun 19, 2002
Messages
63,651
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
Joined
Aug 20, 2009
Messages
278
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>
 

Forum statistics

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

Some videos you may like

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...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top