运行时错误9-订阅超出范围

时间:2019-02-28 07:03:45

标签: excel vba

我不断收到“超出范围”的错误,但是范围的引用是正确的,请帮助我解决错误:在下面提到的代码中,编码中的错误或错误以粗体突出显示:

Sub CopyStuff()

    **Sheets("Data-BNF").Range("D11:X76").Copy**
    Sheets("Storage-OI").Range("C" & Rows.Count).End(xlUp).Offset(2, 0).PasteSpecial xlPasteValues

End Sub

1 个答案:

答案 0 :(得分:0)

修改以下内容,然后尝试:

Option Explicit

Sub Copy()

    Dim ws1 As Worksheet, ws2 As Worksheet
    Dim Lastrow As Long

    With ThisWorkbook
        Set ws1 = .Worksheets("Data-BNF")
        Set ws2 = .Worksheets("Storage-OI")
    End With

    Lastrow = ws2.Cells(ws2.Rows.Count, "C").End(xlUp).Row

    ws1.Range("D11:X76").Copy
    'Try the **ONE of the below**
    ws2.Range("C" & Lastrow + 1).PasteSpecial Paste:=xlPasteValue
    ws2.Range("C" & Lastrow + 1).PasteSpecial xlPasteValues

End Sub