Hello,
I'm struggling with a nested loop and could use some help/guidance on. I have a sheet with a random amount of data (see attached). My goal is to insert a row and concatenate some text based on the cell below its' value. The below code kind of does this portion, but it stops working when it gets to numbers for some reason (i.e. for some reason, when I run the code it is not determining the difference between "1" or "2", but it is recognizing all the other changes in values. This is basically part 2 of the nest.
Part 1 of the nested loop requires me to ignore everything from "StartHeader" through "N" so any values in between will just pass a row count + 1. Once the macro reaches "N" it will know to automatically insert a row below "N" and concatenate "Page " and the value of the Cell immediately below this new line and then move on to the next row and check it's value against the previous row.
Once it detects a change, it again adds a new line and concatenates "Page" and the value of the cell immediately below.
This continues until we reach "StartHeader" where we basically don't do anything until we get back to "N".
sub Nested_Starts
Dim rw As Long
Dim cl As Long
Dim LastRw As Long
rw = 2
cl = 1
Sheets("Sample2").Select
LastRw = Application.CountA(Range("A:A"))
Do While rw <= LastRw
Cells(rw, cl).Select
Cells(rw - 1, cl).Select
'If Cells(rw, cl) = "StartHeader" Then
'rw = rw + 1
'Do Until Cells(rw, cl) = "N"
' Selection.Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
' Cells(rw + 1, cl).Value = "Section" & " " & Cells(rw + 1, cl).Value
'End If
'Loop
'ActiveCell.Select
If Cells(rw, cl) <> Cells(rw - 1, cl) Then
Rows(rw).EntireRow.Insert
Cells(rw, cl).Value = "Section" & " " & Cells(rw + 1, cl).Value
End If
rw = rw + 2
Loop
End Sub
Bookmarks