Hi people! The script that I've written reads the info on a column and if the value is zero, it highlights the value in red and copies the whole row to a new worksheet. The script works, but for some reason, it is also copying many other rows that do not fit the criteria. Anyone have a clue why it does that?
Any help is appreciated, here's the code:
Any help is appreciated, here's the code:
VBA Code:
Sub CheckForZeros()
Dim ws As Worksheet
Dim lastRow As Long
Dim dRange As Range
Dim z0Ws As Worksheet
Dim z0Errors As Range
Dim i As Long
Set ws = ThisWorkbook.Sheets("Upper Beaver_collar")
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row
Set dRange = ws.Range("D2:D" & lastRow)
Set z0Ws = ThisWorkbook.Sheets.Add
z0Ws.Name = "Z_0"
Set z0Errors = z0Ws.Range("A1")
For i = 1 To lastRow - 1
If dRange(i + 1, 1) = 0 Then
dRange(i + 1, 1).Interior.Color = RGB(255, 0, 0)
Dim errorRow As Range
Set errorRow = ws.Rows(i + 1)
errorRow.Copy z0Errors
Set z0Errors = z0Errors.Offset(1, 0)
End If
Next i
End Sub