Hi,
Generally speaking, when you're inserting or deleting rows on a worksheet, it's a LOT easier to loop bottom to top. Otherwise you'll drive yourself crazy trying to keep track of the things to 'skip'.
Try this code:
Sub Better()
Dim i As Long
'inserting/deleting rows works a LOT simpler if you loop bottom to top..
For i = 15 To 3 Step -1 'don't bother to check the top cell, unless you really want an extra row
If Cells(i, 1) <> Cells(i - 1, 1) Then
Worksheets("Sheet2").Cells(i, 1).EntireRow.Insert
End If
Next i
End Sub
To see what's going on in your code, add a couple of DEBUG.Print lines:
Sub Old()
For Each cell In Range("A2:A15")
Debug.Print cell.Address
If cell.Value <> cell.Offset(1, 0).Value Then
cell.Offset(1, 0).EntireRow.Insert
Set cell = cell.End(xlDown)
Debug.Print cell.Address
End If
Next cell
End Sub
You'll see that cell goes to a7 like you want, but the loop through the collection then goes to the next item on it's list, which is a6
Bookmarks