How to add message box to add a specific number of cells or rows

zhead

New Member
Joined
Jul 14, 2016
Messages
17
I have a macro that works great as is but I am trying to add a Message box that will ask for the number of rows to be added between each row of data. the way it is right now it will add 17 rows between each row of data. Can someone please help?
VBA Code:
Sub RunMe()
Dim x As Integer

x = 2

Do
    Rows(x).Resize(16).Insert
    x = x + 17
Loop Until IsEmpty(Cells(x, "A"))
 
End Sub
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
This will do it, you can change the prompt as desired.
VBA Code:
Sub RunMe()
Dim x As Long, y As Long
x = 2
y = InputBox("Insert how many rows?")
Do
    Rows(x).Resize(y).Insert
    x = x + y + 1
Loop Until IsEmpty(Cells(x, "A"))
 
End Sub
 
Upvote 0
Solution
Maybe:
VBA Code:
Sub RunMe()
  Dim xx As Integer, x As Long 'Use Long for rows, columns as integer is ok.
  x = 2
  xx = Application.InputBox("Enter number of rows to add.", "Row Count to Add", Type:=1)
  Do
    Rows(x).Resize(xx-1).Insert
    x = x + xx
  Loop Until IsEmpty(Cells(x, "A"))
End Sub
 
Upvote 0
jasonb75 Thanks that is exactly what i been trying to do. One more question. Is there a way to start with row 2 as my document has headers.
 
Upvote 0
Kenneth yours works just as well. Thanks. I ask you the same question. Is there a way to start with row 2 as my document has headers?
 
Upvote 0
Changing to x = 3 should do the trick.

There is one slight difference between my code and Kenneth's, I followed the description in your first post and inserted the specified number of rows, while it appears that Kenneth has followed your original code and included the data row in the block so that 1 less row is inserted.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,680
Members
449,116
Latest member
HypnoFant

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