Starting a loop on row 3, help.

MurraySMTH

Board Regular
Joined
Jun 30, 2020
Messages
81
Office Version
  1. 2016
Platform
  1. Windows
Hello all. I am using a loop code to add 7 rows in between rows.

The problem is I don't know how to account for the headers. Can someone take a look at my code. I want 7 rows in between each row that has data, but not in between the header and the first line of rows. So the blank rows would start at 3.

VBA Code:
Sub AddRows()
    ScreenUpdating = False

    With ActiveSheet
        LastRow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Dim AddRows As Integer: AddRows = 7

    Dim i As Integer: i = LastRow

    Do While i <> 1
        Rows(i & ":" & i + AddRows - 1).Insert
        i = i - 1
    Loop

    ScreenUpdating = True
End Sub
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
As far as I can see, you just need to change this line
Do While i <> [B]3[/B]
Or a slightly different method which might be a little easier to understand.
VBA Code:
Sub AddRows()
    ScreenUpdating = False

    With ActiveSheet
        lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    End With

    Dim AddRows As Long: AddRows = 7

    Dim i As Long

    For i = lastrow To 3 Step -1
        Rows(i).Resize(AddRows).Insert
    Next

    ScreenUpdating = True
End Sub
Note that I've used Long instead of Integer, with large volumes of data, Integer can potentially result in runtime errors.
 
Upvote 0
Replace this
VBA Code:
    Dim i As Integer: i = LastRow

    Do While i <> 1
        Rows(i & ":" & i + AddRows - 1).Insert
        i = i - 1
    Loop
with
VBA Code:
   Dim i As Long
   For i = lastRow To 3
      Rows(i).Resize(addrows).Insert
   Next i
 
Upvote 0
Thank you Jason and Fluff. That is it. Yes you made it way simpler. I needed to change Dim i as Long... good call.
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,982
Members
449,201
Latest member
Lunzwe73

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top