I have this code that adds rows where I need them based on information in column C.
Dim lRow As Long
For sRow = Cells(Cells.Rows.Count, "C").End(xlUp).Row To 3 Step -1
If Cells(sRow, "C") <> Cells(sRow - 1, "C") Then Rows(sRow).EntireRow.Insert
Next sRow
The result I get for this is that a row is inserted at row 15 (where my table headings are located), moving those table headings down to row 16. Then another row is inserted between table headings and content at row 17. Then rows are inserted at the desired locations from there on.
How can I change this so that this begins at row 16 and only inserts a new row when the content in column C changes?
and here's what I'm getting:
Each time the script runs, it will push the whole table down one row.
CodePudding user response:
If you want to start from row 16, just change the starting point in the for loop
Dim lRow As Long
For sRow = 16 To 3 Step -1
If Cells(sRow, "C") <> Cells(sRow - 1, "C") Then Rows(sRow).EntireRow.Insert
Next sRow
CodePudding user response:
I think I get it. You're just inserting a row where there's a difference in the 'C' column. Unfortunately, that includes your header. Thy this:
Dim lRow As Long
For sRow = Cells(Cells.Rows.Count, "C").End(xlUp).Row To 3 Step -1
If Cells(sRow, "C") <> Cells(sRow - 1, "C") and Cells(sRow - 1, "C").Value <> "Or Ty" Then
Rows(sRow).EntireRow.Insert
End If
Next sRow