Insert Rows and Drag Formulas Down on Multiple Sheets - VBA

jwb1012

Board Regular
Joined
Oct 17, 2016
Messages
167
Hello, I am attempting to get my code to loop through all worksheets that begin with "Labor BOE", find all numbers in column A (beginning in row 2), insert that many rows below the cell with the number, and then drag the formulas down.

For example, if cell A10 = 3, then I need 3 rows to be inserted below. But, after these rows are inserted, I need columns C-J to be dragged down.

I have everything working, except I am unable to get the formulas to drag down. Any thoughts?


Code:
Sub InsertRows()
Dim End_Row As Long, n As Long, Ins As Long
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
    If Left(sh.Name, 9) = "Labor BOE" Then
        
        End_Row = sh.Range("L" & Rows.Count).End(xlUp).Row
        
        Application.ScreenUpdating = False
        For n = End_Row To 3 Step -1
            Ins = sh.Cells(n, "A").Value
            
            If Ins > 0 Then sh.Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
        Next n
        
    End If
Next sh
End Sub
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
You could try this. I don't like it as it involves copy & paste which will probably fail unless you start activating worksheets etc., but see how you get on
Code:
Sub InsertRows()
Dim End_Row As Long, n As Long, Ins As Long
Dim sh As Worksheet
For Each sh In ActiveWorkbook.Sheets
    If Left(sh.Name, 9) = "Labor BOE" Then
        
        End_Row = sh.Range("L" & Rows.Count).End(xlUp).Row
        
        Application.ScreenUpdating = False
        For n = End_Row To 3 Step -1
            Ins = sh.Cells(n, "A").Value
            
            If Ins > 0 Then
                sh.Range("A" & n + 1 & ":A" & n + Ins).EntireRow.Insert
[COLOR=#FF0000]                sh.range("C" & n & ":J" & n).copy destination:= sh.range("C" & n+1 & ":J" & n+ins)
            end if[/COLOR]
        Next n
        
    End If
Next sh
End Sub
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,214
Members
449,074
Latest member
cancansova

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