我从网上获取了这段代码。它将多个Excel文件合并为单个文件(每个文件都在单独的工作表中)。
导入DATA的文件不会自动关闭。这意味着我需要手动关闭8-10个文件并且"不要保存"他们,这需要很多时间。丢失的代码是什么?
Option Explicit
Sub CombineExcelFiles()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
sDelimiter = "|"
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Excel Files (*.*xl*), *.*xl*", _
MultiSelect:=True, Title:="Excel Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
wkbTemp.Sheets(1).Copy
Set wkbAll = ActiveWorkbook
wkbTemp.Close (False)
wkbAll.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=False, _
Other:=True, OtherChar:="|"
x = x + 1
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=False, _
Other:=True, OtherChar:=sDelimiter
End With
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
答案 0 :(得分:1)
wkbTemp.Close False
应该关闭工作簿而不保存它。
但另一个问题是为什么要处理While
循环之外的第一个文件?我认为没有理由这样做。因此,我们可以将该代码缩短为:
Option Explicit
Public Sub CombineExcelFiles()
Dim FilesToOpen
Dim x As Integer
Dim wkbAll As Workbook
Dim wkbTemp As Workbook
Dim sDelimiter As String
On Error GoTo ErrHandler
Application.ScreenUpdating = False
sDelimiter = "|"
FilesToOpen = Application.GetOpenFilename _
(FileFilter:="Excel Files (*.*xl*), *.*xl*", _
MultiSelect:=True, Title:="Excel Files to Open")
If TypeName(FilesToOpen) = "Boolean" Then
MsgBox "No Files were selected"
GoTo ExitHandler
End If
x = 1
Set wkbAll = ActiveWorkbook
While x <= UBound(FilesToOpen)
Set wkbTemp = Workbooks.Open(Filename:=FilesToOpen(x))
With wkbAll
wkbTemp.Sheets(1).Move After:=.Sheets(.Sheets.Count)
wkbTemp.Close False
.Worksheets(x).Columns("A:A").TextToColumns _
Destination:=Range("A1"), DataType:=xlDelimited, _
TextQualifier:=xlDoubleQuote, _
ConsecutiveDelimiter:=False, _
Tab:=False, Semicolon:=False, _
Comma:=False, Space:=False, _
Other:=True, OtherChar:=sDelimiter
End With
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Set wkbAll = Nothing
Set wkbTemp = Nothing
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
我建议尽可能避免使用ActiveWorkbook
因为它不是已定义的工作簿,而是目前处于活动状态的任何工作簿。另请注意,ActiveWorkbook
和ThisWorkbook
之间存在差异(这是一个已定义的工作簿。此时代码运行的是工作簿)。*
另一件事是.Worksheets(x)
可以是,但不一定是实际移动的工作表。我会说因为你在最后一张纸之后移动新纸张,你还需要访问最后一张纸:.Worksheets(.Worksheets.Count)
。
使用Sheets
和Worksheets
之间也存在差异。 Sheets
集合包含工作表以及图表等,但Worksheets
集合仅包含工作表。因此,您应该决定哪一个是正确的,我建议您始终使用Worksheets
,除非您确实需要Sheets
。
我认为没有必要将变量设置为空。
Set wkbAll = Nothing
Set wkbTemp = Nothing
如果我没有完全错误,那么Excel会在程序结束时自动执行此操作。