Forum Discussion

Roshan_K's avatar
Roshan_K
Copper Contributor
Jan 14, 2021

Check if a value in a worksheet is Odd or Even

Hi,

 

Can anyone please help me to understand the VBA code for checking the value in any particular worksheet whether its Odd or Even by using For Next or For each loop in Excel VBA.

 

Thanks

1 Reply

  • Roshan_K 

    Here is an example. Instead of the MsgBox lines, insert the code you want to execute in each situation.

    Sub Test()
        Dim cell As Range
        For Each cell In Worksheets("Sheet1").Range("A2:A15")
            If IsNumeric(cell.Value) Then
                If cell.Value Mod 2 = 0 Then
                    MsgBox "The value is even"
                Else
                    MsgBox "The value is odd"
                End If
            Else
                MsgBox "The value is not numeric"
            End If
        Next cell
    End Sub

Resources