Macro to copy row and paste row in a different sheet based on user input

Buns1976

Board Regular
Joined
Feb 11, 2019
Messages
194
Office Version
  1. 365
Platform
  1. Windows
Hi Everyone,

How would one modify the code below to paste to a row in a different sheet for example copy Row 2 in Sheet "Cost"
and Paste/Insert in Row 5 in Sheet "Profit"?

Thank you,


VBA Code:
Sub v()
Dim c, d
c = Application.InputBox("Enter the row number to be copied.", Type:=1)
If TypeName(c) = "Boolean" Then Exit Sub
d = Application.InputBox("Enter the row number where to insert.", Type:=1)
If TypeName(c) = "Boolean" Then Exit Sub
Rows(Val(c)).Copy
Rows(Val(d)).Insert
Application.CutCopyMode = False
End Sub
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Change these lines from:
VBA Code:
Rows(Val(c)).Copy
Rows(Val(d)).Insert
to this:
Code:
Sheets("Cost").Rows(Val(c)).Copy
Sheets("Profit").Rows(Val(d)).Insert
 
Upvote 0
Change these lines from:
VBA Code:
Rows(Val(c)).Copy
Rows(Val(d)).Insert
to this:
Code:
Sheets("Cost").Rows(Val(c)).Copy
Sheets("Profit").Rows(Val(d)).Insert
Good Lord. Don't know why I didn't see that? THANK YOU VERY MUCH!
 
Upvote 0
Change these lines from:
VBA Code:
Rows(Val(c)).Copy
Rows(Val(d)).Insert
to this:
Code:
Sheets("Cost").Rows(Val(c)).Copy
Sheets("Profit").Rows(Val(d)).Insert
Wondering if there is a way to select multiple rows rather that just a single row? Maybe something like "Start Row, End Row"?
 
Upvote 0
Have a try, added some error-checking but no sure it's bullet-proof (if necessary you may add/fix that ;)).
VBA Code:
Sub v()
    Dim c, d
    Dim flRow() As String
    Dim fRow, lRow
    c = Application.InputBox("Enter 'row number' to be copied." & vbLf & "For multiple rows use format 'first-last'.", "Input Row Copy")
    If TypeName(c) = "Boolean" Then Exit Sub
    flRow = Split(c, "-")
    fRow = flRow(0)
    On Error Resume Next
    lRow = flRow(1)
    On Error GoTo 0
    If lRow = "" Then lRow = fRow
    d = Application.InputBox("Enter the row number where to insert.", "Destination Row")
    If TypeName(d) = "Boolean" Then Exit Sub
    Sheets("Cost").Range(Val(fRow) & ":" & Val(lRow)).EntireRow.Copy
    Sheets("Profit").Rows(Val(d)).Insert
    Application.CutCopyMode = False
    Exit Sub
End Sub
 
Upvote 0
Have a try, added some error-checking but no sure it's bullet-proof (if necessary you may add/fix that ;)).
VBA Code:
Sub v()
    Dim c, d
    Dim flRow() As String
    Dim fRow, lRow
    c = Application.InputBox("Enter 'row number' to be copied." & vbLf & "For multiple rows use format 'first-last'.", "Input Row Copy")
    If TypeName(c) = "Boolean" Then Exit Sub
    flRow = Split(c, "-")
    fRow = flRow(0)
    On Error Resume Next
    lRow = flRow(1)
    On Error GoTo 0
    If lRow = "" Then lRow = fRow
    d = Application.InputBox("Enter the row number where to insert.", "Destination Row")
    If TypeName(d) = "Boolean" Then Exit Sub
    Sheets("Cost").Range(Val(fRow) & ":" & Val(lRow)).EntireRow.Copy
    Sheets("Profit").Rows(Val(d)).Insert
    Application.CutCopyMode = False
    Exit Sub
End Sub
PERFECT. Thank you kindly!
 
Upvote 0

Forum statistics

Threads
1,215,831
Messages
6,127,148
Members
449,364
Latest member
AlienSx

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