|
本帖最后由 ykcbf1100 于 2024-12-9 19:58 编辑
多文件合并
- Sub ykcbf() '//2024.12.9
- Set fso = CreateObject("scripting.filesystemobject")
- Set List = CreateObject("System.Collections.ArrayList")
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Dim tm: tm = Timer
- Dim rng As Range
- Set ws = ThisWorkbook
- Set sh = ws.Sheets("Sheet1")
- For Each sht In Sheets
- If sht.Name <> sh.Name Then sht.Delete
- Next
- p = ThisWorkbook.Path & ""
- For Each f In fso.GetFolder(p).Files
- If LCase$(f.Name) Like "*[0-9_0-9]*" Then
- fn = Split(fso.GetBaseName(f), "_")
- fn = fn(1)
- List.Add fn
- End If
- Next f
- List.Sort
- For Each k In List
- n = n + 1
- Set sht = ws.Sheets.Add(After:=ws.Sheets(ws.Sheets.Count))
- sht.Name = "A" & CNtoW(n)
- ReDim brr(1 To 10000, 1 To 5)
- m = 0
- For Each f In fso.GetFolder(p).Files
- If LCase$(f.Name) Like "*[0-9_0-9]*" Then
- fn = Split(fso.GetBaseName(f), "_")
- If fn(1) = k Then
- Set wb = Workbooks.Open(f, 0)
- With wb.Sheets(1)
- Set rng = .UsedRange
- Call qc(rng)
- arr = rng.Value
- End With
- wb.Close 0
- For i = 3 To UBound(arr)
- If arr(i, 2) <> Empty Then
- m = m + 1
- For j = 1 To UBound(arr, 2)
- brr(m, j) = arr(i, j)
- Next
- End If
- Next
- With sht
- .[b1].Resize(, 3) = Split(arr(1, 1))
- .Columns(1).NumberFormatLocal = "h:mm"
- .[a2].Resize(m, 5) = brr
- End With
- End If
- End If
- Next f
- Next
- sh.Activate
- Set d = Nothing
- Application.ScreenUpdating = True
- MsgBox "共用时:" & Format(Timer - tm) & "秒!"
- End Sub
- Function qc(rng As Range)
- Set reg = CreateObject("VBScript.Regexp")
- With reg
- .Pattern = "[\s\x7F-\xA0]+"
- For Each rn In rng
- rn.Value = .Replace(rn.Value, "")
- Next
- End With
- End Function
- Function CNtoW(ByVal num As Long) As String
- CNtoW = Replace(Cells(1, num).Address(False, False), "1", "")
- End Function
复制代码
|
|