Basic Exercise
In this exercise, you have to classify the column "A" numbers as an even number or an odd number.
Remember that in most cases there is more than one way to solve the exercise.
Hint: Remember that even numbers when divided by 2 do not have remainders (the remainder equals to zero).
Declare the variables and find the last data entry in the worksheet
Dim LastRow As Long
Dim CellVal As Long
Dim i As Integer
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
Make a loop For Next until the last entry line. Determine If a number is even or odd using de Mod operator by dividing that number by 2
For i = 3 To LastRow
CellVal = Cells(i, 1).Value
If CellVal Mod 2 = 1 Then
Cells(i, 2).Value = "Odd"
Else
Cells(i, 2).Value = "Even"
End If
Next i
Sub Solution()
Dim LastRow As Long
Dim CellVal As Long
Dim i As Integer
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For i = 3 To LastRow
CellVal = Cells(i, 1).Value
If CellVal Mod 2 = 1 Then
Cells(i, 2).Value = "Odd"
Else
Cells(i, 2).Value = "Even"
End If
Next i
End Sub
SuperExcelVBA.com is learning website. Examples might be simplified to improve reading and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. All Rights Reserved.
Excel ® is a registered trademark of the Microsoft Corporation.
© 2019 SuperExcelVBA | ABOUT
Thank you for contributing. A message was sent reporting your comment.