问题描述:若干个工作簿,每个工作簿里有一个工作表,如何利用VBA把若干工作簿里的表保存到一个新建的工作簿里,并单独成表?
VBA:用 Dir() 函数完成多工作簿合并
一 合并之前的文件夹和文件
文件夹
二 代码
Sub 多工作簿合并_Dir函数方法()
Dim fName As String, fPath As String, suffStr As String, newFile As Workbook, opFile As Workbook, newSht As Worksheet
Application.ScreenUpdating = False
Set newFile = Workbooks.Add
fPath = "E:\Zhuomian_CJ\ZZZ\VBA\答网友问"
fName = Dir(fPath & "\", vbNormal)
Do While fName <> ""
suffStr = Split(fName, ".")(UBound(Split(fName, ".")))
If suffStr Like "xls*" Then
Set opFile = Workbooks.Open(fPath & "/" & fName)
'Debug.Print fName, suffStr
Set newSht = newFile.Worksheets.Add(after:=newFile.Sheets(newFile.Sheets.Count))
opFile.Sheets(1).UsedRange.Copy newSht.Range("a1")
newFile.ActiveSheet.Name = Left(fName, Len(fName) - Len(suffStr) - 1) & "_" & opFile.Sheets(1).Name
Set newSht = Nothing
opFile.Close xlDoNotSaveChanges
Set opFile = Nothing
End If
fName = Dir
Loop
With newFile
.SaveAs fPath & "/合并文件.xlsx"
.Close xlSaveChanges
End With
Application.ScreenUpdating = True
MsgBox "合并完成!"
Set newFile = Nothing
End Sub
三 执行代码后的文件效果
有任何【Office办公软件/办公自动化/文件批处理】的问题,欢迎和我交流!
是若干个excel文件的合并,还是一个文件的若干个表的合并?都可以,网上有实例,搜一下,可以新建一个表,再读取其他的表,写进来,
我用VB做了个工具,可以快速实现这个功能,
选择多文件工作表汇总
在这个界面可以按需求填入参数,就可以啦。
可以到这里看看
https://mp.csdn.net/mp_blog/creation/editor/128047296
vba合并工作簿,https://blog.csdn.net/hhhhh_51/article/details/123834366