Add Specific number of rows based on cell value

Utradeshow

Well-known Member
Joined
Apr 26, 2004
Messages
800
Office Version
  1. 365
Hi All,

I have this code that adds rows based on the value of cell A2. I need two things. I would like the code to run after the value is entered in cell A2 vs having to press a add button. and also would like it to add rows starting from row 11 down? Is that possible?

VBA Code:
Sub InsertRow()

  Dim ws As Worksheet
  Dim NBOFROWS As Range
  Set ws = ThisWorkbook.ActiveSheet

  With ws
    Set NBOFROWS = .Range("A2")
    ActiveCell.EntireRow.Offset(1).Resize(NBOFROWS.Value).Insert shift:=xlDown
  End With

End Sub
 
TTI Agent Rate Calculator v1.01 - TEST.xlsm
ABCDEFGHIJKLMNOPQRSTUVWXYZAAABACADAEAFAG
1
2
3ADD ROWS10DOUBLE CLICK TO COPY DOWN
4LocationStore NameRegionStore TypeCityStZipAddressZipAgent NameCity State Airport CodeMiles UNITSHOURS (4) MINHELPER REQUIREDSTORAGE REQUIREDDISPOSAL REQUIREDHandling IN26' STRAIGHT / DRIVERDRIVER (OT)HELPERHELPER (OT)STORAGESTORAGE OVERSIZEHandling OUTDISPOSALUDFUDFUDFFUEL TOTAL COST
5C1WEST HARTFORDWest HartfordCT06110999 New Park Avenue06095TESTWindsorCTBDL914NOYESNO$25.00$480.00$0.00$0.00$0.00$25.00$0.00$25.00$0.00$0.00$0.00$0.00$75.00$630.00
6C2NEWINGTONNewingtonCT061111000 New Park Avenue06095TESTWindsorCTBDL1314NOYESNO$25.00$480.00$0.00$0.00$0.00$25.00$0.00$25.00$0.00$0.00$0.00$0.00$75.00$630.00
7C3MANCHESTERManchesterCT060421001 New Park Avenue06095TESTWindsorCTBDL914NOYESNO$25.00$480.00$0.00$0.00$0.00$25.00$0.00$25.00$0.00$0.00$0.00$0.00$75.00$630.00
8C4NORTH HAVENNorth HavenCT064731002 New Park Avenue06801TESTBethelCTBDL2814NOYESNO$25.00$480.00$0.00$0.00$0.00$25.00$0.00$25.00$0.00$0.00$0.00$0.00$75.00$630.00
9C5ORANGEOrangeCT064771003 New Park Avenue06801TESTBethelCTBDL2014NOYESNO$25.00$480.00$0.00$0.00$0.00$25.00$0.00$25.00$0.00$0.00$0.00$0.00$75.00$630.00
10C6WATERBURYWaterburyCT067051004 New Park Avenue06801TESTBethelCTBDL2414NOYESNO$25.00$480.00$0.00$0.00$0.00$25.00$0.00$25.00$0.00$0.00$0.00$0.00$75.00$630.00
11C7WATERFORDWaterfordCT063851005 New Park Avenue06095TESTWindsorCTBDL4414NOYESNO$25.00$480.00$0.00$0.00$0.00$25.00$0.00$25.00$0.00$0.00$0.00$0.00$75.00$630.00
12
13
14
15
16
17
18
19
20
21
227GRAND TOTAL$4,410.00
ROLLOUT
Cell Formulas
RangeFormula
K5:K11K5=VLOOKUP(I5,AGENTS!A:C,3,FALSE)
L5:L11L5=VLOOKUP(I5,AGENTS!A:D,4,FALSE)
M5:M11M5=VLOOKUP(I5,AGENTS!A:E,5,FALSE)
N5:N11N5=CDXDistance(G5,I5)
T5:T11T5=VLOOKUP(I5,AGENTS!A:G,7,FALSE)*O5
U5:U11U5=VLOOKUP(I5,AGENTS!A:K,11,FALSE)*P5
AA5:AA11AA5=VLOOKUP(I5,AGENTS!A:I,9,FALSE)*O5
AB5AB5=IF(S5="YES",VLOOKUP(I5,AGENTS!A:R,18,FALSE),0)*O5
AB6:AB11AB6=IF(Q6="YES",VLOOKUP(I6,AGENTS!A:R,18,FALSE),0)*O6
AF5:AF11AF5=VLOOKUP(I5,AGENTS!A:R,17,FALSE)
AG5:AG11AG5=SUM(T5:AF5)
I5:I11I5=CDXClosestZip(G5,0,AGENTS!$A$2:$A$165)*1
W5:W11W5=IF(Q5="YES",VLOOKUP(I5,AGENTS!A:M,13,FALSE),0)
Y5:Y11Y5=IF(R5="YES",(VLOOKUP(I5,AGENTS!A:O,15,FALSE)*O5),0)
O22,AG22O22=SUBTOTAL(9,O5:O21)


VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("C3")) Is Nothing Or Range("C3") = "" Then Exit Sub
    Application.EnableEvents = False
    ActiveCell.EntireRow.Offset(8).Resize(Range("C3").Value).Insert Shift:=xlDown
    Application.EnableEvents = True
End Sub


Here is the actual sheet, I did change the ranges a little as the sheet changed.
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
So in which row are the formulas to be copied? In post #8 you said they were in row 9. Now with the new layout it should be row 3 but I don't see any.
 
Upvote 0
So in which row are the formulas to be copied? In post #8 you said they were in row 9. Now with the new layout it should be row 3 but I don't
Yes, from row 3 down copying the formulas in row 2. I was trying to show the result of the current code.
 
Upvote 0
Tweak your macro to this:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("C3")) Is Nothing Or Range("C3") = "" Then Exit Sub
    Application.EnableEvents = False
    Range("A2").EntireRow.Copy
    Range("A5").EntireRow.Resize(Range("C3").Value).Insert Shift:=xlDown
    Application.CutCopyMode = False
    Application.EnableEvents = True
End Sub
Formulas in row 2 need to reference the same row (example for cell Y2):
=IF(R2="YES",(VLOOKUP(I2,Agents!A:O,15,FALSE)*O2),0)
so they can be correctly pasted in row 5 and on.
 
Upvote 0
Tweak your macro to this:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("C3")) Is Nothing Or Range("C3") = "" Then Exit Sub
    Application.EnableEvents = False
    Range("A2").EntireRow.Copy
    Range("A5").EntireRow.Resize(Range("C3").Value).Insert Shift:=xlDown
    Application.CutCopyMode = False
    Application.EnableEvents = True
End Sub
Formulas in row 2 need to reference the same row (example for cell Y2):
=IF(R2="YES",(VLOOKUP(I2,Agents!A:O,15,FALSE)*O2),0)
so they can be correctly pasted in row 5 and on.
Ok, Now were talkin! However, I do get #VALUE! as a result until I click into each cell and press enter again. It seems to be because the formula involves CDX ZipStream addin in Column I. If I replace with a value, it then copies all the normal formulas just fine.

Here is the formula in COLUMN I =@CDXClosestZip(G5,0,AGENTS!$A$2:$A$165)*1
 
Upvote 0
Actually, Cancel that last request! That was a user error. It works perfectly! Thank you so much for your time and extra effort. :)
 
Upvote 0
Yes, I was aware that would be possible issues with the Functions of columns I and N but since you didn't post them there was no way to check. Instead, have you checked the GRAND TOTAL in column AG and the one in column O ?
Anyway, thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0
Yes, I was aware that would be possible issues with the Functions of columns I and N but since you didn't post them there was no way to check. Instead, have you checked the GRAND TOTAL in column AG and the one in column O ?
Anyway, thanks for the positive feedback(y), glad having been of some help.
The Grand total columns do not work, is there a fix for that?
 
Upvote 0
This could be a solution as long as there is no other data below the total in column AG:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim LastRow As Long                           '<- added
    If Intersect(Target, Range("C3")) Is Nothing Or Range("C3") = "" Then Exit Sub
    Application.EnableEvents = False
    Range("A2").EntireRow.Copy
    Range("A5").EntireRow.Resize(Range("C3").Value).Insert Shift:=xlDown
    LastRow = Range("AG" & Rows.Count).End(xlUp).Row '<- added
    Range("AG" & LastRow).Formula = "=SUM(AG5:AG" & LastRow - 1 & ")" '<- added
    Range("O" & LastRow).Formula = "=SUM(O5:O" & LastRow - 1 & ")" '<- added
    Application.CutCopyMode = False
    Application.EnableEvents = True
End Sub
 
Upvote 0
This could be a solution as long as there is no other data below the total in column AG:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim LastRow As Long                           '<- added
    If Intersect(Target, Range("C3")) Is Nothing Or Range("C3") = "" Then Exit Sub
    Application.EnableEvents = False
    Range("A2").EntireRow.Copy
    Range("A5").EntireRow.Resize(Range("C3").Value).Insert Shift:=xlDown
    LastRow = Range("AG" & Rows.Count).End(xlUp).Row '<- added
    Range("AG" & LastRow).Formula = "=SUM(AG5:AG" & LastRow - 1 & ")" '<- added
    Range("O" & LastRow).Formula = "=SUM(O5:O" & LastRow - 1 & ")" '<- added
    Application.CutCopyMode = False
    Application.EnableEvents = True
End Sub
Yes sir! That is beautiful, Thank you again!
 
Upvote 0

Forum statistics

Threads
1,221,841
Messages
6,162,314
Members
451,759
Latest member
damav78

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