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 :)

[TABLE="width: 500"]
<tbody>[TR]
[TD][/TD]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]Cust:A111[/TD]
[TD]3[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]4[/TD]
[TD]Dr. Mr. Smith,[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]The following invoices are now past due.[/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]7[/TD]
[TD="align: right"]#[/TD]
[TD]Document #[/TD]
[TD]Amt[/TD]
[/TR]
[TR]
[TD]8[/TD]
[TD="align: right"]1[/TD]
[TD]123456[/TD]
[TD]71.00[/TD]
[/TR]
[TR]
[TD]9[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
 

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
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

Forum statistics

Threads
1,223,898
Messages
6,175,272
Members
452,628
Latest member
dd2

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