|
Sub CombineSheets()
Dim wbSource As Workbook
Dim wbTarget As Workbook
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Dim folderPath As String
Dim fileName As String
Dim NewSheetName As String
Dim i As Integer
Dim FileExt As String
' 此处替换为你的文件夹路径
folderPath = "C:\Users\Administrator\Downloads\文件\"
savePath = "C:\Users\Administrator\Downloads\文件\合并.xlsx"
Application.ScreenUpdating = False
fileName = Dir(folderPath & "*.xls*")
Set wbTarget = Workbooks.Add
Do While fileName <> ""
Set wbSource = Workbooks.Open(folderPath & fileName)
For Each wsSource In wbSource.Sheets
FileExt = Mid(fileName, InStrRev(fileName, ".") + 1)
NewSheetName = Left(fileName, Len(fileName) - Len(FileExt) - 1) & "_" & wsSource.Name
wsSource.Copy After:=wbTarget.Sheets(wbTarget.Sheets.Count)
Set wsTarget = wbTarget.Sheets(wbTarget.Sheets.Count)
wsTarget.Name = NewSheetName
Next wsSource
wbSource.Close SaveChanges:=False
fileName = Dir
Loop
' 保存新的工作簿
wbTarget.SaveAs savePath
' 关闭新的工作簿
wbTarget.Close SaveChanges:=False
Application.ScreenUpdating = True
MsgBox "所有工作表已成功复制到新文件中!"
End Sub
Sub CombineSheets()
Dim wbSource As Workbook
Dim wbTarget As Workbook
Dim wsSource As Worksheet
Dim wsTarget As Worksheet
Dim folderPath As String
Dim fileName As String
Dim NewSheetName As String
Dim i As Integer
Dim FileExt As String
' 此处替换为你的文件夹路径
folderPath = "C:\Users\Administrator\Downloads\文件\"
savePath = "C:\Users\Administrator\Downloads\文件\合并.xlsx"
Application.ScreenUpdating = False
fileName = Dir(folderPath & "*.xls*")
Set wbTarget = Workbooks.Add
Do While fileName <> ""
Set wbSource = Workbooks.Open(folderPath & fileName)
For Each wsSource In wbSource.Sheets
FileExt = Mid(fileName, InStrRev(fileName, ".") + 1)
NewSheetName = Left(fileName, Len(fileName) - Len(FileExt) - 1) & "_" & wsSource.Name
wsSource.Copy After:=wbTarget.Sheets(wbTarget.Sheets.Count)
Set wsTarget = wbTarget.Sheets(wbTarget.Sheets.Count)
wsTarget.Name = NewSheetName
Next wsSource
wbSource.Close SaveChanges:=False
fileName = Dir
Loop
' 保存新的工作簿
wbTarget.SaveAs savePath
' 关闭新的工作簿
wbTarget.Close SaveChanges:=False
Application.ScreenUpdating = True
MsgBox "所有工作表已成功复制到新文件中!"
End Sub
|
-
|