Insert Rows with Msg Box

RandyD123

Active Member
Joined
Dec 4, 2013
Messages
296
Office Version
  1. 2016
Platform
  1. Windows
I need to have a msg box pop up on my macro that asks "How Many Rows Would You Like To Insert?" Right now I only have this much:

Code:
Sub InsertBlankRows()
Application.ScreenUpdating = False
numRows = InputBox("How many Rows")
'Inserting a Row at at Row 5
Range("A5").EntireRow.Insert
Application.ScreenUpdating = True
End Sub

Right now it will insert only 1 row after row 5. I need it to ask me how many rows I want to insert after row 5. Any help would be appreciated. Thank you in advance
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Try
Code:
Sub InsertBlankRows()
Application.ScreenUpdating = False
numRows = InputBox("How many Rows")
If numRows <= 0 Then Exit Sub
'Inserting a Row at at Row 5
Rows(6 & ":" & 6 + numRows - 1).Insert 'inserts row after row 5
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi,
try this update to your code:


Code:
Sub InsertBlankRows()
    Dim numRows As Variant
    
    Do
        numRows = InputBox("How many Rows?", "Insert Rows", 1)
'cancel pressed
        If StrPtr(numRows) = 0 Then Exit Sub
    Loop Until Val(numRows) > 0
    Application.ScreenUpdating = False
        Range("A5").EntireRow.Resize(Val(numRows)).Insert
    Application.ScreenUpdating = True
End Sub


Dave
 
Upvote 0
Works Perfectly. Thank You


Try
Code:
Sub InsertBlankRows()
Application.ScreenUpdating = False
numRows = InputBox("How many Rows")
If numRows <= 0 Then Exit Sub
'Inserting a Row at at Row 5
Rows(6 & ":" & 6 + numRows - 1).Insert 'inserts row after row 5
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Maybe I spoke too soon!!! It seems that if I don't put a number of rows in the msg box and just cancel it, I get a Run-time error '13': There is no option to debug, the only option is to "end" and it does with no further errors.
 
Upvote 0
This should work If you want you can change the if so it has a message box like this

Code:
If numrows <= 0 Or Len(numrows) = 0 Then
    MsgBox "cancled or invalid number entered"
    Exit Sub
End If


Code:
Sub InsertBlankRows()
Application.ScreenUpdating = False
numrows = InputBox("How many Rows")
If numrows <= 0 Or Len(numrows) = 0 Then Exit Sub
'Inserting a Row at at Row 5
Rows(6 & ":" & 6 + numrows - 1).Insert 'inserts row after row 5
Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
Maybe I spoke too soon!!! It seems that if I don't put a number of rows in the msg box and just cancel it, I get a Run-time error '13': There is no option to debug, the only option is to "end" and it does with no further errors.

did you try my suggestion?

Dave
 
Upvote 0
Yes, this works perfectly. Thanks again for all the help. Both you and Dave provided great 100% working examples.


This should work If you want you can change the if so it has a message box like this

Code:
If numrows <= 0 Or Len(numrows) = 0 Then
    MsgBox "cancled or invalid number entered"
    Exit Sub
End If


Code:
Sub InsertBlankRows()
Application.ScreenUpdating = False
numrows = InputBox("How many Rows")
If numrows <= 0 Or Len(numrows) = 0 Then Exit Sub
'Inserting a Row at at Row 5
Rows(6 & ":" & 6 + numrows - 1).Insert 'inserts row after row 5
Application.ScreenUpdating = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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