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

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
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:
Upvote 0
Thanks alot.

It didnt quite do what i needed but the tip was in the resize(BlankRows) which did the job for me perfectly.
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,214,606
Messages
6,120,483
Members
448,967
Latest member
visheshkotha

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