【发布时间】:2019-07-30 13:46:13
【问题描述】:
需要将 Excel 工作簿导入现有 Excel 启用宏的工作簿的工作表
已使用指定目标的代码成功导入我使用“.xlsx”作为目标,因为“*”用于指定文件夹中找到的所有 xlsx 文件。
Sub CopySheets()
Dim WB As Workbook
Dim SourceWB As Workbook
Dim WS As Worksheet
Dim ASheet As Worksheet
'Turns off screenupdating and events:
Application.ScreenUpdating = False
Application.EnableEvents = False
'Sets the variables:
Set WB = ActiveWorkbook
Set ASheet = ActiveSheet
Set SourceWB = Workbooks.Open(WB.Path & "\*.xlsx") 'Modify to match
'Copies each sheet of the SourceWB to the end of original wb:
For Each WS In SourceWB.Worksheets
WS.Copy after:=WB.Sheets(WB.Sheets.Count)
Next WS
SourceWB.Close savechanges:=False
Set WS = Nothing
Set SourceWB = Nothing
WB.Activate
ASheet.Select
Set ASheet = Nothing
Set WB = Nothing
Application.EnableEvents = True
End Sub
错误是“运行时错误'1004':抱歉,我们找不到 C:\Users\ZMOLD01\Desktop\CaseStudy Results*.xlsx。它可能被移动、重命名或删除了吗?
【问题讨论】:
-
使用
Dir函数。你不能在Workbooks.Open中使用这样的通配符。请参阅this question 了解如何操作。 -
您发送的只是打开文件本身而不是将其导入当前的Excel工作表中。