No problems, glad I could help
I've commented the code I posted. Just to be clear, when I speak of a
preceding row in my comments, I mean
the row above the active row,
not the row that came before. I hope that makes sense. If anyone else wants to add to my comments, please feel free. I don't have a great understanding of Excel and its objects.
Code:
Public Sub InsertRowAtSet()
' Written for TSG user Dreambringer; inserts a blank row
' after a set of numbers in column A (skips rows that
' have distinct/unique values in column A).
' I'll take for granted the variable declarations are
' pretty self-explanatory. I use lngCurRow to keep
' track of the row I'm on, and lngCounter to increment
' (or, in this case, decrement) through sets of identical
' numbers.
Dim Rng As Range
Dim lngCurRow As Long
Dim lngCounter As Long
' This isn't necessary but saves a lot of processor.
Application.ScreenUpdating = False
' Based on Zack's (firefytr) forward-compatibility model; sets
' the working range to the used cells in column A by grabbing
' the whole column (A1:A65536) and moving up once, which moves
' to the first populated cell from the bottom of the sheet.
Set Rng = Range("A1:A" & Cells(Rows.Count, "A").End(xlUp).Row)
' We set the current row lngCurRow to the last row in the range,
' which has the same row number as the count of all rows in the range.
' We're decrementing by 1 (or incrementing by -1) until row 2. If
' we went to row 1, it would throw an error at row 1, because there's
' no row 0.
For lngCurRow = Rng.Rows.Count To 2 Step -1
' If the Value of the Cell in column 1 of the current row =
' the Value of the Cell in column 1 of the current row offset -1
' (think: the cell immediately above), then we know we've hit a set of
' identical numbers.
If Rng.Cells(lngCurRow, 1).Value = Rng.Cells(lngCurRow, 1).Offset(-1, 0).Value Then
' First, insert a row below the current row. It has to be below
' the current row because the current row represents the last row
' of the set of identical numbers. This is because the current row's
' value is equal to the value of the row above it.
Rng.Cells(lngCurRow, 1).Offset(1, 0).EntireRow.Insert Shift:=xlDown
lngCounter = -1
' lngCounter, our decrement counter, manages the offsets. This
' Do While...Loop compares the current row's value to preceding rows
' without changing the current row. As soon as the loop hits a
' preceding row that is not equal to the current row, it stops.
' That way, our counter lngCounter tells us how many rows are in the
' set of identical numbers.
Do While Rng.Cells(lngCurRow, 1).Value = Rng.Cells(lngCurRow, 1).Offset(lngCounter, 0).Value
lngCounter = lngCounter - 1
Loop
' I have no idea why I had to add the + 1 to get this to work.
' And I still don't, though I imagine it has something to do with the
' fact that we inserted a row. It may seem odd that we're adding
' lngCurRow and lngCounter since we're decrementing, but remember
' lngCounter is a negative number, so we're actually subtracting.
' The point is, we're changing the current row to the current row
' minus the number of rows in the set of identical numbers.
lngCurRow = lngCurRow + lngCounter + 1
End If
' If the current row's value does not match the preceding row's value,
' the For...Next loop iterates to the preceding row (remember, Step -1).
Next lngCurRow
' Now we get to see the results.
Application.ScreenUpdating = True
End Sub
It'd probably help to copy/paste into a code editor so the comments appear green. Anyway, glad I could help.
chris.