|
- Sub ykcbf() '//2025.4.12 多文件多表排重汇总
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set d = CreateObject("Scripting.Dictionary")
- p = ThisWorkbook.Path & ""
- Set sh = ThisWorkbook.Sheets("Sheet1")
- ReDim brr(1 To 10000, 1 To 100)
- m = 3: n = 1
- brr(1, 1) = "工作簿名"
- brr(2, 1) = "工作表名"
- brr(3, 1) = "姓名"
- Dim tm: tm = Timer
- On Error Resume Next
- For Each f In fso.GetFolder(p).Files
- If LCase$(f.Name) Like "*.xls*" Then
- If InStr(f.Name, ThisWorkbook.Name) = 0 Then
- fn = fso.GetBaseName(f)
- Set wb = Workbooks.Open(f, 0)
- For Each sht In wb.Sheets
- If IsArray(sht.UsedRange) Then
- arr = sht.UsedRange
- n = n + 1
- brr(1, n) = fso.GetBaseName(f)
- brr(2, n) = sht.Name
- For i = 3 To UBound(arr)
- S = arr(i, 2)
- If S <> Empty Then
- If Not d.exists(S) Then
- m = m + 1
- d(S) = m
- brr(m, 1) = S
- End If
- r = d(arr(i, 2))
- brr(r, n) = brr(r, n) + arr(i, 3)
- End If
- Next
- brr(3, n) = arr(2, 3)
- End If
- Next
- wb.Close 0
- End If
- End If
- Next
- n = n + 1: m = m + 1
- brr(1, n) = "行向合计": brr(m, 1) = "列向合计"
- For i = 4 To m - 1
- brr(i, n) = Application.Sum(Application.Index(brr, i))
- Next
- For j = 2 To n
- brr(m, j) = Application.Sum(Application.Index(brr, 0, j))
- Next
- With sh
- .UsedRange.Clear
- .Cells.Interior.ColorIndex = 0
- .[a1].Resize(3, n).Interior.Color = 49407
- .[a4].Resize(m - 3, 1).Interior.Color = 5296274
- .UsedRange.Offset(1, 1).NumberFormatLocal = "0"
- With .[a1].Resize(m, n)
- .Value = brr
- .Borders.LineStyle = 1
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- With .Font
- .Name = "微软雅黑"
- .Size = 11
- End With
- End With
- .Cells(1, n).Resize(3).Merge
- End With
- Application.ScreenUpdating = True
- MsgBox "共用时:" & Format(Timer - tm, "0.000") & "秒!"
- End Sub
复制代码
|
|