我知道这个问题已经被问过很多次了,我尝试使用Google搜索感兴趣的内容,但是找不到正确的代码。 (相信我,我不是接受者)。
无论如何,该想法是运行一个脚本以将所有Excel文件(CAD,GBP,JPY,USD)合并到一个新的Excel文件中(标签显示名称“ CAD”,“ GBP”,“ JPY”,“ USD” )在当前文件夹中。我已经编写了以下脚本来合并Excel文件,但它甚至无法正常工作。
Sub CombineWorkbooks()
Dim Path As String
Dim FileName As String
Dim Wkb As Workbook
Dim WS As Worksheet
Application.EnableEvents = False
Application.ScreenUpdating = False
Path = "X:\PATH\TO\EXCEL\FILES"
FileName = Dir(Path & "\*.xls", vbNormal)
Do Until FileName = ""
Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)
For Each WS In Wkb.Worksheets
WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Next WS
Wkb.Close False
FileName = Dir()
Loop
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
您只有一个小错误。
更改:
Wkb.Close False
收件人:
Wkb.Close SaveChanges:=False
这是我完整的,经过测试的有效解决方案:
Sub CombineWorkbooks()
Dim Path As String
Dim FileName As String
Dim Wkb As Workbook
Dim WS As Worksheet
Application.EnableEvents = False
Application.ScreenUpdating = False
Path = "X:\PATH\TO\EXCEL\FILES"
FileName = Dir(Path & "\*.xls", vbNormal)
Do Until FileName = ""
Set Wkb = Workbooks.Open(FileName:=Path & "\" & FileName)
For Each WS In Wkb.Worksheets
WS.Copy After:=ThisWorkbook.Sheets(ThisWorkbook.Sheets.Count)
Next WS
Wkb.Close SaveChanges:=False
FileName = Dir()
Loop
Application.EnableEvents = True
Application.ScreenUpdating = True
End Sub
如果这不适用于您,请提供有关结果和/或错误的更多详细信息。文件夹中文件名的列表也将很有帮助。
如果这对您有用,请记住将其标记为您的答案,以便其他人知道您有解决方案。谢谢!