Hi ZmeY
Difficult to see what you are trying to achieve here. Are you trying to scroll down a list of values and insert a row between two that are different? e.g.:
1
1
1
<----Insert here
2
2
2
Anyway - some pointers on the code:
You don't need to select something to look at it. You can just refer to its address, or location:
The Rows command requires numbers, or a string. Unfortunately, "row:row2" will not be evaluated correctly. You could build the string:
strRows = Format(row,"0") & ":" & format(row2,"0")
Kind of messy. In this case, since row2 is always 2 rows beneath row, you could use:
Rows(row).Resize(3).Insert
To list through a range of cells, try this:
Dim cel As Range
'Don't use this code for inserting rows - gets too confused!
For Each cel in Range("A1:A50")
' Do something to the cel/row, etc.
Next cel
However, for inserting rows this all gets a bit tricky, as when you insert a row it affects the following rows. You end up in a perpetual loop.
So, start at the bottom and work your way up. Use row numbers, not cells/ranges, as these don't move about. If you insert a row then move above it, you know where you are and the rows you're now working on won'y have been affected by the previous insertion.
Dim i As Single
For i = ActiveSheet.UsedRange.Rows.Count To 2 Step -1
If Rows(i).Cells(1, 1).Value <> Rows(i - 1).Cells(1, 1).Value Then
Rows(i).Insert
End If
Next i
Hope some of this helps.
Cheers, Rob.
Bookmarks