Copy & Paste Value based on input box

mst3kr

New Member
Joined
Apr 15, 2013
Messages
46
Office Version
  1. 2019
  2. 2010
  3. 2007
Platform
  1. Windows
I'm looking for a macro that will copy & paste value a range of cells UP by a certain number of rows based on a value put into an input box.

I have a macro that allows me to copy a row of formulas down based on an input box but I haven't been able to manipulate it to do what I'm after above.

Sub Add_New_Rows_at_Bottom()
Dim LR As Long
Dim j As Variant
Sheets("Sheet1").Select
LR = Cells(Rows.Count, 1).End(xlUp).Row
j = InputBox("Enter number of rows to be added:")
Sheet5.Range("A20:V20").Copy Sheet1.Range("A" & LR + 1).Resize(j)
Sheets("Sheet1").Range("J" & Rows.Count).End(xlUp).Offset(1).Select
End Sub


Any ideas?
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
What do you mean by "up"? Up relative to what? Can you provide an example?

Lets say that LR is row 10 (LR = 10), and the user inputs 5 rows to be added (j = 5). What would be the destination cells for your copy?
 
Upvote 0
What do you mean by "up"? Up relative to what? Can you provide an example?

Lets say that LR is row 10 (LR = 10), and the user inputs 5 rows to be added (j = 5). What would be the destination cells for your copy?

I was imagining that if my data range was A10:A50, if I keyed 5 in the input box, the values from A10:A50 would be copied and then pasted as a value staring in row 5. Or, if I typed 7 in the input box, the values would be pasted starting in row 3...
 
Upvote 0
I was imagining that if my data range was A10:A50, if I keyed 5 in the input box, the values from A10:A50 would be copied and then pasted as a value staring in row 5. Or, if I typed 7 in the input box, the values would be pasted starting in row 3...

Perhaps this.
VBA Code:
Sub Add_New_Rows_Above()
    Dim LR As Long
    Dim j As Variant
    
    Sheets("Sheet1").Activate
    LR = Range("A" & Rows.Count).End(xlUp).Row
    j = InputBox("Enter number of rows to be added:")

    Select Case j
    Case ""
        Exit Sub
    Case Is >= LR
        MsgBox "Please choose a number less than " & LR
        Exit Sub
    End Select

    Sheet5.Range("A10:A50").Copy Sheet1.Range("A" & LR - j)
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

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