I am writing a very complex and long code to have a spreadsheet look like an old form that users are use to looking at. I am running into a problem with a "For I = 2 to n" portion of code. I use this loop in multiple spots. I'm only having an issue with this one loop. For some reason it is ending before "I" gets to "n." If you look at the image 1 I have attached. Row 3 was added because Column R information. Every time the information changes in Column R, the code is suppose to add a row like Row 3. I'm having issues with image 2 I have attached. It adds Row 5888, but then the loop ends. Even though it recognizes n as Row 5898 and I as 5889.
VBA Code:
'Sorts System Column
For I = 3 To n
b = Cells(I, 1).Row
c = Cells(b, 1).End(xlDown).Row
Range("A" & b, "S" & c).Sort key1:=Columns(18)
n = EDRBIS.Cells(EDRBIS.Rows.Count, 14).End(xlUp).Row
I = Cells(I, 1).End(xlDown).Row
Next I
'Issue with this portion
'Add System Title Row
For I = 2 To n
If IsEmpty(.Cells(I, 18)) Then
Rows(I + 1).Insert
Cells(I + 2, 18).Copy Destination:=.Cells(I + 1, 2)
Range("A" & I + 1, "S" & I + 1).Interior.Color = RGB(211, 211, 211)
I = I + 2
ElseIf Cells(I, 18) <> Cells(I - 1, 18) Then
Rows(I).Insert
Cells(I + 1, 18).Copy Destination:=.Cells(I, 2)
Range("A" & I, "S" & I).Interior.Color = RGB(211, 211, 211)
I = I + 1
End If
n = EDRBIS.Cells(EDRBIS.Rows.Count, 14).End(xlUp).Row + 1
Next I