PDA

View Full Version : [SOLVED:] Macro to insert rows based on cell value



Maverick21
07-05-2016, 02:06 PM
Newb here. I'm trying to create a template that will add a number of rows above row 20 based on a value input by the user in cell E5. So far I have this which will be built on to but i need this part to work first. The +18 piece is just to make it select the right row given the input in E5.

For example, User inputs the number of units he wants an ROI for, and the spreadsheet adds the appropriate number of rows to this range.



Sub Add_Rows()
'
' Add_Rows Macro
'
Application.ScreenUpdating = False
Dim intRowInsert As Integer
intRowInsert = Range("E5").Value + 18
Rows("20:inRowInsert").Insert

End Sub

mdmackillop
07-05-2016, 02:59 PM
You were not far away with your code

Sub Add_Rows()
'
' Add_Rows Macro
'
Application.ScreenUpdating = False
Dim intRowInsert As Integer
intRowInsert = Range("E5").Value + 18
Rows("20:" & intRowInsert).EntireRow.Insert

End Sub



or something like


Sub Add_Rows2()
Cells(19, 1).Resize([E5]).EntireRow.Insert
End Sub

'or
Sub Add_Rows3()
Cells(18, 1).Offset([E5]).Resize([E5]).EntireRow.Insert
End Sub

Maverick21
07-05-2016, 03:08 PM
You were not far away with your code

Sub Add_Rows()
'
' Add_Rows Macro
'
Application.ScreenUpdating = False
Dim intRowInsert As Integer
intRowInsert = Range("E5").Value + 18
Rows("20:" & intRowInsert).EntireRow.Insert

End Sub



or something like


Sub Add_Rows2()
Cells(19, 1).Resize([E5]).EntireRow.Insert
End Sub

'or
Sub Add_Rows3()
Cells(18, 1).Offset([E5]).Resize([E5]).EntireRow.Insert
End Sub




Awesome! Thanks for the help!