VBA insert multiple rows

bilsko

New Member
Joined
May 29, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
I'd like to insert multiple blank rows into my excel data which is thousands of rows long. The amount of blank rows needed to to be input will be from a value in the cell thats activated.
At the moment i have code that only does 1 row which is shown below but its only a start.
Any help would be appreciated.

VBA Code:
Sub InsertBlankRowsBasedOnCellValue()

    Dim Col As Variant
    Dim BlankRows As Long
    Dim LastRow As Long
    Dim R As Long
    Dim StartRow As Long

        Col = "A"
        StartRow = 1
        BlankRows = 1

            LastRow = Cells(Rows.Count, Col).End(xlUp).Row

            Application.ScreenUpdating = False

            With ActiveSheet
For R = LastRow To StartRow + 1 Step -1
If .Cells(R, Col) = "1" Then
.Cells(R + 1, Col).EntireRow.Insert Shift:=xlDown
End If
Next R
End With
Application.ScreenUpdating = True

End Sub
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
See if this modification does what you want.

VBA Code:
Sub InsertBlankRowsBasedOnCellValue()
    Dim Col As Variant
    Dim BlankRows As Long
    Dim LastRow As Long
    Dim R As Long
    Dim StartRow As Long
    Col = "A"
    StartRow = 1   
    LastRow = Cells(Rows.Count, Col).End(xlUp).Row
    Application.ScreenUpdating = False
        With ActiveSheet
            For R = LastRow To StartRow + 1 Step -1
                If .Cells(R, Col) = "1" Then
                    BlankRows = .Cells(R, Col)l.Value
.                   Cells(R + 1, Col).Resize(BlankRows).EntireRow.Insert Shift:=xlDown
                End If
            Next R
        End With
    Application.ScreenUpdating = True
End Sub

It was assumed the
cell thats activated
meant the cell currently addressed by the For loop, since tthe active cells could be anywhere.
 
Last edited:

bilsko

New Member
Joined
May 29, 2020
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Thanks alot.

It didnt quite do what i needed but the tip was in the resize(BlankRows) which did the job for me perfectly.
 

JLGWhiz

Well-known Member
Joined
Feb 7, 2012
Messages
12,979
Office Version
  1. 2013
Platform
  1. Windows
Thanks alot.

It didnt quite do what i needed but the tip was in the resize(BlankRows) which did the job for me perfectly.
Year, I wasn't quite sure what you had in the cells in column A, but you got the idea and that's what counts.
Regards, JLG
 

Watch MrExcel Video

Forum statistics

Threads
1,129,791
Messages
5,638,329
Members
417,021
Latest member
moon miner

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
Top