Insert "X" number of Rows Based on User Input

activeman

New Member
Joined
Mar 1, 2011
Messages
15
Hi<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:eek:ffice:eek:ffice" /><o:p></o:p>
<o:p> </o:p>
Using Excel VBA I am looking to automate a basic Copy and Paste routine which amounts to little more than copying row 3 and then pasting it in the next blank row below.<o:p></o:p>
<o:p> </o:p>
However I want the User to decide on the “number” of rows they want pasting into the Worksheet based on an input box.<o:p></o:p>
<o:p> </o:p>
Based on this “number” the macro will copy row 3 find the next blank row and copy row 3 the applicable “number” of times as determined by the User.<o:p></o:p>
<o:p> </o:p>
E.g. <o:p></o:p>
Run Macro<o:p></o:p>
Input Box asks User how many rows they would like pasting?<o:p></o:p>
User Inputs “50”<o:p></o:p>
Macro copies Row 3 and then pastes row 3 into the next blank row 50 times over<o:p></o:p>
<o:p> </o:p>
Any help would be much appreciated?<o:p></o:p>
 

Some videos you may like

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)

Mike Blackman

Well-known Member
Joined
Jun 8, 2007
Messages
2,494
Hi,

Maybe something like this?

Code:
Sub InsertRows()

    Dim lngRows As Long, lngNextRow As Long

    On Error GoTo Finish

    lngRows = CLng(InputBox("How many rows do you wish to insert?"))
    lngNextRow = Range("A" & Rows.Count).End(xlUp).Row + 1
    
    Rows(3).Copy Rows(lngNextRow & ":" & lngNextRow + lngRows - 1)
       
Finish:
    If Err.Number <> 0 Then MsgBox Prompt:="Please ensure you only enter numeric values!"

End Sub
 

activeman

New Member
Joined
Mar 1, 2011
Messages
15
Hi,

Maybe something like this?

Code:
Sub InsertRows()
 
    Dim lngRows As Long, lngNextRow As Long
 
    On Error GoTo Finish
 
    lngRows = CLng(InputBox("How many rows do you wish to insert?"))
    lngNextRow = Range("A" & Rows.Count).End(xlUp).Row + 1
 
    Rows(3).Copy Rows(lngNextRow & ":" & lngNextRow + lngRows - 1)
 
Finish:
    If Err.Number <> 0 Then MsgBox Prompt:="Please ensure you only enter numeric values!"
 
End Sub
I am attempting to adjust the above code so rather than copying and pasting row 3 it copies and pastes the FORMULA's ONLY from row 3.

Not getting too far would anyone have any ideas how to do this?
 

Watch MrExcel Video

Forum statistics

Threads
1,100,209
Messages
5,473,170
Members
406,849
Latest member
gustavor0

This Week's Hot Topics

Top