Home » excel » Excel VBA For loop with variable index not working

Excel VBA For loop with variable index not working

Posted by: admin May 14, 2020 Leave a comment

Questions:

My data set looks like this

Name    ID

Alice   1

Alice   2

Alice   -4

Bob    4

Bob    7

2344   9

2344   10

4I    99

4I   04

What I want it to look like is this:

Name    ID

Alice   1

Alice   2

Alice   -4



Name    ID

Bob    4

Bob    7




Name    ID

2344   9

2344   10



Name    ID

4I    99

4I   04

Every time the name entry changes, I want to introduce 1 empty rows and a row of my header.

    Sub insertingrows()


  FinalColumn = Cells(1, Columns.Count).End(xlToLeft).Column
  FinalRow = Cells(Rows.Count, 1).End(xlUp).Row

  For j = 3 To FinalRow

    If Cells(j, 2) <> Cells(j - 1, 2) Then
        'Rows(j & ":" & j + 2).Insert
        Rows(j).Insert
        Rows(1).Copy
        Rows(j + 1).Insert
        FinalRow = FinalRow + 2
        j = j + 2
    End If

  Next j



End Sub

My initial actual data has 3830 rows but even though I am updating my variable FinalRow inside the loop, the loop is still stopping around 3830 and is leaving a lot of the process unfinished. Is this a limitation of For? Should I use some other type of loop?

How to&Answers:

Let’s say your worksheet looks like this

enter image description here

Put this code in a module

Option Explicit

Sub Sample()
    Dim ws As Worksheet
    Dim lRow As Long, i As Long

    Set ws = ThisWorkbook.Sheets("Sheet1")

    Application.ScreenUpdating = False

    With ws
        lRow = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = lRow To 3 Step -1
            If .Cells(i, 1).Value <> .Cells(i - 1, 1).Value Then
                .Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
                .Cells(i, 1).Value = "Name": .Cells(i, 2).Value = "ID"
                .Rows(i).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            End If
        Next i
    End With

    Application.ScreenUpdating = True
End Sub

Output

enter image description here

Answer:

The bounds of a For loop are calculated once, when the loop begins, and then become flat numbers. If you want to change the bounds, fall back to a Do loop:

j = 3
Do

    If Cells(j, 2) <> Cells(j - 1, 2) Then
        'Rows(j & ":" & j + 2).Insert
        Rows(j).Insert
        Rows(1).Copy
        Rows(j + 1).Insert
        FinalRow = FinalRow + 2
        j = j + 3
    End If

Loop While j < FinalRow