使用输入框复制范围并粘贴到特定工作表上的固定范围

时间:2015-12-07 08:03:47

标签: excel vba excel-vba

您好,我有这个代码,允许我复制自定义范围并将该范围粘贴到指定的工作表上。但是,当为该指定工作表插入固定范围时,我无法这样做。例如,下面的代码粘贴链接到工作表2但我希望它能够粘贴到工作表2的范围(B2:N5)的链接。如何做到这一点?非常感谢一些帮助!

Sub WorkingDuoFunctionCode()
    Dim rng As Range, inp As Range
    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
    End If

1 个答案:

答案 0 :(得分:1)

Option Explicit

Sub WorkingDuoFunctionCode()

Dim rng As Range, inp As Range

On Error Resume Next

Set rng = Nothing
Set inp = Selection
inp.Interior.ColorIndex = 37

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
    inp.Copy
    Sheets(2).Range(rng.Address).Value = inp.Value
End If
Application.CutCopyMode = False

End Sub

OR

Option Explicit

Sub WorkingDuoFunctionCode()

Dim rng As Range, inp As Range

On Error Resume Next

Set rng = Nothing
Set inp = Selection
inp.Interior.ColorIndex = 37

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
    inp.Copy
    Sheets(2).Range("B2:N5").Value = inp.Value
End If
Application.CutCopyMode = False

End Sub

但这种方式变得毫无用处 - 你只需要2个范围进行复制 - 粘贴,现在你得到3 - 选择,输入框的范围和硬编码的B2:N5。无论如何,你知道的更好。