How to insert blank rows based on inserted number

bfrantic

New Member
Joined
Nov 22, 2011
Messages
6
Hi guys,
My objective is:
When I insert the number of people 10, the excel automatically insert 10 rows from rows number 5 to number 15? Could you please help me for writing the Macro???
Thanks
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Try

Code:
Sub test()
Dim aRow As Variant
aRow = Application.InputBox("Enter number of rows", Type:=1)
If TypeName(aRow) = "Boolean" Then Exit Sub
Rows(5).Resize(aRow).Insert
End Sub
 
Upvote 0
I really appreciate for your fast reply. It's really perfect :)

Just 2 questions:
1- I want to insert the number of desired rows in cell not in Message box; For example I want to ask in cell A1: "How many people" and I want to write the number in A2. Could u plz help me

2-I need when I insert the number, excell insert the rows automatically but now I have to open Macro and run it. How can I assign Macro to a cell?

Thanks a lot for your reply in advance
 
Upvote 0
Try this: right click the sheet tab, select View Code and paste in

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aRow As Variant
If Target.Address(False, False) = "A2" Then
    aRow = Target.Value
    Rows(5).Resize(aRow).Insert
End If
End Sub
 
Upvote 0
Thanks Peter,

It's around 6 hours that I am looking for this code and now in a few minutes you solved my problem. Just one more question :confused:

Now based on your perfect code, Excel has generated for example x rows, now I want excel automatically number this column from 1 to x. Is there any code for that?

I wish the best for you and I became one of your crazy fans...:)
Thanks!
 
Upvote 0
Maybe like this

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim aRow As Variant, i As Long
If Target.Address(False, False) = "A2" Then
    aRow = Target.Value
    Rows(5).Resize(aRow).Insert
    For i = 5 To 5 + aRow - 1
        Range("A" & i).Value = i
    Next i
End If
End Sub
 
Upvote 0
Hi
Actually I faced to a problem; when I put the number of required rows in A2 (For example 3) and excel make 3 rows for me and number it from 1 to 3, but if I find that I have made a mistake and I need 4 rows, Excel make more 4 rows and again from 1 to 4, however totally I need 4 rows.
I want when I modify the number of rows, final rows would be based on modified one only not past No and current one. I really appreciate all your kind supports
Best Regards,
 
Upvote 0

Forum statistics

Threads
1,216,116
Messages
6,128,932
Members
449,480
Latest member
yesitisasport

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