|
如果不合题意,请上传附件说明:
- Sub 按照文件名合并工作簿()
- Dim Fso As Object, Folder As Object, wb As Workbook, arr$(), m&, i&, p$, d As Object, ds As Object
- Set d = CreateObject("scripting.dictionary")
- Set ds = CreateObject("scripting.dictionary")
- p = ThisWorkbook.Path & "\需要得到的结果"
- Set Fso = CreateObject("Scripting.FileSystemObject")
- Set Folder = Fso.GetFolder(ThisWorkbook.Path)
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Call GetFiles(Folder, arr, m, p)
- For i = 1 To m
- With Workbooks.Open(arr(2, i) & "" & arr(1, i))
- If Not d.Exists(arr(1, i)) Then
- .Worksheets(1).Copy
- ds(ActiveWorkbook.Name) = p & "" & arr(1, i)
- Set d(arr(1, i)) = ActiveWorkbook
- Else
- .Worksheets(1).Copy After:=d(arr(1, i)).Worksheets(d(arr(1, i)).Worksheets.Count)
- End If
- ActiveSheet.Name = Split(arr(2, i), "")(UBound(Split(arr(2, i), "")))
- .Close 0
- End With
- Next
- For Each wb In Workbooks
- If ds.Exists(wb.Name) Then wb.Close True, ds(wb.Name)
- Next
- Application.ScreenUpdating = True
- MsgBox "ok"
- Set Folder = Nothing
- Set Fso = Nothing
- End Sub
- Sub GetFiles(ByVal Folder As Object, arr$(), m&, p$)
- Dim SubFolder As Object
- Dim File As Object
- If Folder.Path <> ThisWorkbook.Path And Folder.Path <> p Then
- For Each File In Folder.Files
- If File.Name Like "*.xlsx" Then
- m = m + 1
- ReDim Preserve arr(1 To 2, 1 To m)
- arr(1, m) = File.Name
- arr(2, m) = Folder.Path
- End If
- Next
- End If
- For Each SubFolder In Folder.SubFolders
- Call GetFiles(SubFolder, arr, m, p)
- Next
- End Sub
复制代码 |
评分
-
1
查看全部评分
-
|