Copy paste data from one sheet to any other worksh

2019-07-23 17:48发布

Hi there I have this code which only copy-paste data from sheet 1 one to sheet 2.

Sub CopyPasteCumUpdate()
    Dim rng As Range, inp As Range
    'to remove 0 values that may be a result of a formula or direct entry.
    Set rng = Nothing
    Set inp = Selection
    inp.Interior.ColorIndex = 37

    On Error Resume Next
    Set rng = Application.InputBox("Copy to", Type:=8)
    On Error GoTo 0

    If TypeName(rng) <> "Range" Then
        MsgBox "Cancelled", vbInformation
        Exit Sub
    Else
        rng.Parent.Activate
        rng.Select
        inp.Copy
        Worksheets("Sheet2").Paste Link:=True
        Application.CutCopyMode = 0
    End If
End Sub

Is it possible to input data and paste it to any other sheet without having to hard code?

1条回答
We Are One
2楼-- · 2019-07-23 18:18

From your comment that is easy. Just change this part:

rng.Parent.Activate
rng.Select
inp.Copy
Worksheets("Sheet2").Paste Link:=True
Application.CutCopyMode = 0

With this:
Edit: This is to paste the link in the range the user selected.

Application.Goto rng
inp.Copy
rng.Parent.Paste Link:=True
Application.CutCopyMode = 0
查看更多
登录 后发表回答