在代码上获取粘贴方法失败错误

时间:2015-11-19 04:05:24

标签: excel vba excel-vba

我对此代码存在一些问题,因此它会在wS.Range("B1").Paste Link:=True上显示错误消息。此代码允许用户选择一个范围,该范围应粘贴在工作簿的每个工作表上,在指定的行范围B1到N1中。它表示粘贴方法失败。尝试包括整个范围("B1:N1")但仍然得到相同的错误。一些帮助会很好。谢谢!

   Sub test()
      Dim Rng As Range, _
        Inp As Range, _
        wS As Worksheet

    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
        Inp.Copy

        For Each wS In ActiveWorkbook.Worksheets
            wS.Range("B1").Paste Link:=True
        Next
    End If

    Application.CutCopyMode = 0

End Sub

1 个答案:

答案 0 :(得分:0)

以下内容如何:

Sub test()
      Dim Rng As Range, _
        Inp As Range, _
        wS As Worksheet


    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
        Inp.Copy

        For Each wS In ActiveWorkbook.Worksheets
            Inp.Copy wS.Range(Rng(1).Address)
        Next
    End If

    Application.CutCopyMode = 0

End Sub