Need to add rows when a number is entered

asokaw

New Member
Joined
Jun 24, 2013
Messages
39
Office Version
  1. 2013
Hi
I need to accept a number to cell A1 & insert that number of rows to a set of data already typed
Assume I have already entered data for rows 2 - 20 & when I enter 3 in cell A1 it should insert 3 new blank rows before row 2. Need a simple macro to do this
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Right-click on the sheet tab name at the bottom of the screen that you want to apply this to, select View Code, and paste this code in the resulting VB Editor window:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If Target.Address = "$A$1" Then
        If Target > 0 Then
            Rows("2:" & Target.Value + 1).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        End If
    End If
        
End Sub
Then, anytime anyone enter a number in A1, it will automatically insert that many rows, starting at row 2.
 
Upvote 0
Thanks a lot for the answer it works, will you be able to tell me how I could expand it to all the rows in that column, meaning when No 2 is entered in A1 it will insert 2 rows starting from A2 which happens now, I need to allow to enter No's in A2,A3,A4 & so on & the same thing to happen
 
Upvote 0
Try this:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    
    If (Target.Count = 1) And (Target.Column = 1) Then
        If Target.Value > 0 Then
            Application.EnableEvents = False
            Rows(Target.Row + 1 & ":" & Target.Row + Target.Value).Insert Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
            Application.EnableEvents = True
        End If
    End If
        
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,164
Messages
6,170,444
Members
452,326
Latest member
johnshaji

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