VBA从一个工作表复制到另一个工作表以及最后一行

时间:2018-11-23 13:16:50

标签: excel vba excel-vba

我有28个电子表格,全部位于不同的文件夹中。每个电子表格都有一个平面列表,我需要提取一个文档并每4周更新一次。我已经编写了下面的宏,计划将其嵌入每个电子表格中,以复制到“ Planned Loads.xlsx”,但是我需要将其粘贴到最后一行。我尝试了很多事情,但无法正常工作。有人可以帮我吗?

提前谢谢! :)

    Sub Copy_PlannedLoads()
'
' Copy_PlannedLoads Macro
'
Dim x As Workbook
Dim y As Workbook
Dim Last_Row As Long

'## Open both workbooks first:
Set x = ActiveWorkbook
Set y = Workbooks.Open("C:\Users\peterha\Google Drive\Athlete Development Team\Sport Science\PDMS Data\Planned Load.xlsx")

'Now, copy what you want from x:
x.Sheets("Trimp Load Data NEW").Range("PlannedLoad").Copy

'Now, paste to y worksheet:
y.Sheets("Sheet1").Range("A2").PasteSpecial Paste:=xlPasteValues, Operation:=xlNone
Last_Row = Range("A" & Rows.Count).End(xlUp).Row

'Close y:
y.Close

End Sub

1 个答案:

答案 0 :(得分:0)

使用数组变量更简单。

Sub Copy_PlannedLoads()
    '
    ' Copy_PlannedLoads Macro
    '
    Dim vDB As Variant
    Dim x As Workbook
    Dim y As Workbook
    Dim rngT As Range
    Dim Last_Row As Long

    '## Open both workbooks first:
    Set x = ActiveWorkbook
    vDB = x.Sheets("Trimp Load Data NEW").Range("PlannedLoad")
    Set y = Workbooks.Open("C:\Users\peterha\Google Drive\Athlete Development Team\Sport Science\PDMS Data\Planned Load.xlsx")

    'Now, copy what you want from x:
    'x.Sheets("Trimp Load Data NEW").Range("PlannedLoad").Copy

    'Now, paste to y worksheet:
    Set rngT = y.Sheets("Sheet1").Range("A" & Rows.Count).End(xlUp)(2)
    rngT.Resize(UBound(vDB, 1), UBound(vDB, 2)) = vDB

    'Close y:
    y.Close

End Sub