Macro to add rows based on cell value- multiple tabs

Ashley1432

New Member
Joined
Aug 10, 2017
Messages
8
I am creating customer collections letters and need to be able to add rows of data into the letter based on how many past due invoices the customer has. Each customer has its own tab, and the number of past due invoices is in cell B3 on each tab. I need a macro that will go to each tab and add X many rows (starting at row 8) based on the value in B3 on that specific tab. I'm horrible at macros and try to avoid them but I can't think of a way to do this otherwise. Please help! I will also need it to copy the equations down from row 7 if you want to help with that too :)

ABC
1Cust:A1113
2
3
4Dr. Mr. Smith,
5The following invoices are now past due.
6
7#Document #Amt
8112345671.00
9

<tbody>
</tbody>
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Welcome to the forums!

Give this code a shot - be sure to back up your file before running:

Code:
Public Sub InsertRows()
Dim ws      As Worksheet
Dim insRow  As Long
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
    insRow = ws.Range("B1").Value
    ws.Range("A8").Resize(insRow, 1).EntireRow.Insert shift:=xlDown
Next ws
Application.ScreenUpdating = True
End Sub

As for the part about copying formulas, what cell(s) hold the formulas and where should they be copied to?
 
Upvote 0
This seems to be working, except I do get an error that says Run Time error 1004, Application defined ot object defined error. The line that highlights when I debug is ws.Range("A8").Resize(insRow, 1).EntireRow.Insert shift:=xlDown.

About the copying of the formulas- I have formulas in A7:C7 that i would want copied down into the newly created rows to populate the list of invoices that are past due.

Thanks so much for your help!
 
Upvote 0
This seems to be working, except I do get an error that says Run Time error 1004, Application defined ot object defined error. The line that highlights when I debug is ws.Range("A8").Resize(insRow, 1).EntireRow.Insert shift:=xlDown.

About the copying of the formulas- I have formulas in A7:C7 that i would want copied down into the newly created rows to populate the list of invoices that are past due.

Thanks so much for your help!

The below code works for me on dummy data.

Code:
Public Sub InsertRows()
Dim ws      As Worksheet
Dim insRow  As Long
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
    insRow = ws.Range("B1").Value
    ws.Range("A8").Resize(insRow, 1).EntireRow.Insert shift:=xlDown
    ws.Range("A7:C7").Copy Destination:=ws.Range("A8").Resize(insRow, 3)
Next ws
Application.ScreenUpdating = True
End Sub

If you get that error again, I wonder if it is coming across a worksheet which does not have a value in B1, and ergo is trying to resize the range to an invalid dimension. Hover over insRow in the yellow-highlighted line if you get the error again and let me know what value it is returning.
 
Upvote 0
I see whats happening, one of the tabs has 0 in B1 and so its stopping, not know what to do. When i hover over the insRow it says insRow=0. Other than that it is working beautifully!
 
Upvote 0
The below code will check to see if the value is greater than 0. If it is, then it will execute the code. If it is not, it will go to the next worksheet.

Code:
Public Sub InsertRows()
Dim ws      As Worksheet
Dim insRow  As Long
Application.ScreenUpdating = False
For Each ws In ActiveWorkbook.Worksheets
    insRow = ws.Range("B1").Value
    If insRow > 0 Then
        ws.Range("A8").Resize(insRow, 1).EntireRow.Insert shift:=xlDown
        ws.Range("A7:C7").Copy Destination:=ws.Range("A8").Resize(insRow, 3)
    End If
Next ws
Application.ScreenUpdating = True
End Sub
 
Upvote 0
This worked beautifully! Thanks so much for your help, i never would have been able to do this on my own. :)
 
Upvote 0

Forum statistics

Threads
1,215,026
Messages
6,122,743
Members
449,094
Latest member
dsharae57

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