Excel合并多个文件
Sub CombineWorkbooks()
Dim FilesToOpen
Dim x As Integer
On Error GoTo ErrHandler
Application.ScreenUpdating = False
FilesToOpen = Application.GetOpenFilename(FileFilter: = "MicroSoft Excel文件(*.xls),*.xls",MultiSelect: = True,Title: = "要合并的文件")
If TypeName(FilesToOpen) = "Boolean" then
MsgBox "没有选中文件"
Goto ExitHandler
end if
x = 1
While x <= UBound(filestoopen)
Workbooks.Open fileName: = filestoopen(x)
Sheets().Move After: = ThisWorkbook.Sheets (ThisWorkbook.Sheets.Count)
x = x + 1
Wend
ExitHandler:
Application.ScreenUpdating = True
Exit Sub
ErrHandler:
MsgBox Err.Description
Resume ExitHandler
End Sub
根据文件类型,xls,csv等均可。需要先建立一个xls文件,在宏代码里面,运行上述代码,得到结果。
sheet 1----view code(查看代码),将上述代码COPY到打开的新窗口中,
2009-2-19 18:04:00 上传
下载附件 (1.33 MB)
如何用VBA函数合并多个文件