Insert given numbers of rows above selected row

gopal baheti

New Member
Joined
Dec 2, 2014
Messages
34
Need to Insert rows given in Cell A1 above active row.
If i select row no 1000 and cell A1 Contains 5 then insert 5 rows above row no. 1000.
Regards
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,343
Office Version
  1. 2021
Platform
  1. Windows
Try this:

Code:
Sub Insert_Rows()
Application.ScreenUpdating = False
Dim ans As Long
Dim nr As Long
nr = Selection.Row
ans = Range("A1").Value
Rows(nr).Resize(ans).Insert xlShiftUp
Application.ScreenUpdating = True
End Sub
 
Upvote 0

My Aswer Is This

Well-known Member
Joined
Jul 5, 2014
Messages
19,343
Office Version
  1. 2021
Platform
  1. Windows
This modified version assures a numeric value is in Range ("A1")
And that the value is greater then (0)
Code:
Sub Insert_Rows()
Application.ScreenUpdating = False
Dim ans As Long
Dim nr As Long
nr = Selection.Row
    If IsNumeric(Range("A1")) = True And Range("A1") > 0 Then
        ans = Range("A1").Value
        Rows(nr).Resize(ans).Insert xlShiftUp
        Else
        MsgBox "Inproper value in Range(A1)"
    End If
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,191,685
Messages
5,988,002
Members
440,125
Latest member
vincentchu2369

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
Top