Excel VBA:将工作表复制到现有工作簿(变量)

时间:2018-08-02 19:37:33

标签: excel vba excel-vba

我想将活动工作簿存储为变量,以便即使名称更改也可以调用同一工作簿。然后,我想打开另一个工作簿并将第一张工作表复制到现有工作簿中。

以下是相关部分:

Dim WorkbookA As Workbook
     Set WorkbookA = ActiveWorkbook.Name


folder = Application.GetOpenFilename("Excel (*.xlsx), *.xlsx", 1, "Select file")
Workbooks.Open Filename:=folder

    NewFile = Dir(folder)
    Workbooks(NewFile).Activate

ActiveSheet.Select
ActiveSheet.Copy Before:=Workbooks(WorkbookA).Sheets(1)

我不断收到的错误消息与将WorkbookA设置为活动工作簿有关。

非常感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

.Name是工作簿对象的字符串(属性)。因此,您肯定会在那一行上得到一个错误。

只需:Set workbookA = ActiveWorkbook。我也会这样做:

Set workbookA = ActiveWorkbook

Dim otherWorkbook as Workbook

folder = Application.GetOpenFilename("Excel (*.xlsx), *.xlsx", 1, "Select file")
Set otherWorkbook = Workbooks.Open(Filename:=folder)
workbookA.ActiveSheet.Copy Before:=otherWorkbook.Sheets(1)