Question No. 21 VBA Code For Practice
Question No. 21 VBA Code For Practice
Sub DisplayEmployeesOver2Years()
Dim wsNew As Worksheet
Dim wsData As Worksheet
Dim lastRow As Long
Dim i As Long
Dim yearsOfWork As Long
' Loop through the data and copy employees with over 2 years of service to the
new worksheet
For i = 2 To lastRow
yearsOfWork = DateDiff("yyyy", wsData.Cells(i, 6).Value, Date)
If yearsOfWork >= 2 Then
wsData.Rows(i).Copy
Destination:=wsNew.Rows(wsNew.Cells(wsNew.Rows.Count, 1).End(xlUp).Row + 1)
End If
Next i