Используйте long
, а не диапазон и повторяйте его.
'you need to put as type after each variable
Dim worksheet1 As Worksheet, worksheet2 As Worksheet
Dim i As Long, lr As Long
Set worksheet1 = ActiveWorkbook.Worksheets("Content")
Set worksheet2 = ActiveWorkbook.Worksheets("Book")
With worksheet1
lr = .Cells(.Rows.Count, 4).End(xlUp).Row 'get last row
End With
With worksheet2
'make sure the second sheet isn't longer than the first
If .Cells(.Rows.Count, 8).End(xlUp).Row > i Then
lr = .Cells(.Rows.Count, 8).End(xlUp).Row
End If
End With
With worksheet1
For i = 1 To lr 'iterate the row
If .Cells(i, 4).Value <> worksheet2.Cells(i, 8).Value Then
.Cells(i, 4).Interior.Color = vbRed
MsgBox "Data does not match, please check again!", vbOKOnly
Exit Sub
Else
.Cells(i, 4).Interior.Color = vbGreen
End If
Next i
End With