将数据从一个工作簿复制到另一工作簿的最后一行

时间:2019-03-17 07:05:17

标签: excel vba

下面是我的代码,用于将数据从一个工作簿复制到另一个工作簿,但这给了我错误:

  

下标超出范围

此代码也不会将数据复制到目标表的最后一行。

year

1 个答案:

答案 0 :(得分:0)

编辑您的代码,应该可以:

Sub CopyDataNEW()
    Dim sBook_t As Workbook
    Dim sBook_s As Workbook
    Dim wbPath_t As String
    Dim wbPath_s As String
    Dim lRow2 as Long
    'make sure file destination is correct, file name is correct, and file extension is correct here
    'you will get subscript out of range error here if these are not accurate
    wbPath_t = "F:\TESt\DB_BA.xlsx"
    wbPath_s = "F:\TESt\PPC_BA.xlsm"

    Set sBook_s = Workbooks.Open(wbPath_s)
    Set sBook_t = Workbooks.Open(wbPath_t)
    'Find last row in paste workbook
    lRow2 = sBook_t.Sheets("cstdatalist").Range("A" & sBook_t.Sheets("cstdatalist").Rows.Count).End(xlUp).Row + 1
    'copy and paste
    sBook_s.Sheets("cstdata").Range("A2:EK2").Copy
    sBook_t.Sheets("cstdatalist").Range("A" & lRow2).Paste
    'clear clip board
    Application.CutCopyMode = False
    'clear objects
    Set sBook_s = Nothing
    Set sBook_t = Nothing

End Sub
相关问题