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
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Paste this updated version of your macro into the sheet's module:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A2")) Is Nothing Or Range("A2") = "" Then Exit Sub
    Application.EnableEvents = False
    ActiveCell.EntireRow.Offset(8).Resize(Range("A2").Value).Insert Shift:=xlDown
    Application.EnableEvents = True
End Sub
Instead, if you need it to work on every sheet of your workbook paste it into the ThisWorkbook module and change its name to:
Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
 
Last edited:
Upvote 0
That works amazing! I wanted to see if it would be easy to change the code to insert rows and copy down the formulas instead of just inserting rows starting from row 10 down
 
Upvote 0
If the formulas are in row 10 use this:
VBA Code:
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
    If Intersect(Target, Range("A2")) Is Nothing Or Range("A2") = "" Then Exit Sub
    Application.EnableEvents = False
    ActiveCell.EntireRow.Offset(7).Copy
    ActiveCell.EntireRow.Offset(8).Resize(Range("A2").Value).Insert Shift:=xlDown
    Application.CutCopyMode = False
    Application.EnableEvents = True
End Sub
 
Upvote 0
Thanks for the positive feedback(y), glad having been of some help.
 
Upvote 0
I know this has been a while, But can this VBA be adjusted to copy down the formulas in the row above as well?
 
Upvote 0
to copy down the formulas in the row above as well

Please explain better; your formulas are in row 10, the macro copies the formula, inserts the new rows from row 11 pushing down whatever is already there.
What do you mean with "in the row above" ? which row are you referring to ?
 
Upvote 0
Yes, I have the first 10 rows, 1 Header row and 9 normal rows. The 9 rows contain formulas. When I enter say "100" in A2 the VBA inserts 100 rows after row 10, however, I lose all the formulas. Maybe if I could have it insert the 100 rows and then fill the formulas in?
 
Upvote 0
Oh, but this doesn't match with what I supposed in post #4 and post #8 so please attach a example of your cell layout and formulas with XL2BB LINK
 
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