HI all,
Is it possible to change the ‘end’ value of a for loop whilst in the actual loop?
The reason I’m asking is that I’m looping through some cells in a spreadsheet and inserting rows depending on certain conditions. I’m calculating the ‘last row’ of the spreadsheet using this piece of script:
finalrow = xlSht3.Cells(xlApp.Rows.Count, 6).End(xlUp).Row
I’m then using this value in a for loop as shown. Because I’m inserting rows, I need to tell the loop to now stop at the ‘new’ final row, not the original one. The problem seems to be that the loop still stops at the original value and not my updated value. Any ideas?
TotalColumnLength = 1 For RowStart = 3 To finalrow If xlSht3.Cells(RowStart + 1, 2) = “” Then TotalColumnLength = TotalColumnLength + 1 Else If TotalColumnLength 3 Then xlSht3.Cells(RowStart + 1, 2).EntireRow.Select If TotalColumnLength = 1 Then xlApp.Selection.Insert Shift:=xlDown xlApp.Selection.Insert Shift:=xlDown RowStart = RowStart + 2 finalrow = finalrow + 2 Else xlApp.Selection.Insert Shift:=xlDown RowStart = RowStart + 1 finalrow = finalrow + 1 End If TotalColumnLength = 1 Else TotalColumnLength = 1 End If End If Next RowStart