|
更新一下,解決增加部悶無法匯總的問題- Sub ykcbf() '//2024.8.3 按部门汇总
- Dim arr, brr, d, p, f
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Dim tm: tm = Timer
- Set fso = CreateObject("Scripting.FileSystemObject")
- Set d = CreateObject("scripting.dictionary")
- Set d1 = CreateObject("scripting.dictionary")
- b = [{1,2,3,4,5,6}]
- Set sh = ThisWorkbook.Sheets("采购计划表")
- c = sh.UsedRange.Find("*预计月度用量合计*", , , , , 1).Column
- p = ThisWorkbook.Path & ""
- ReDim brr(1 To 10000, 1 To 15)
- 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 = Split(fso.GetBaseName(f), "(")(1)
- ' fn = Left(fn, Len(fn) - 1)
- Set wb = Workbooks.Open(f, 0)
- With wb.Sheets("月度使用计划表")
- r = .Cells(.Rows.Count, 1).End(3).Row
- arr = .[a1].Resize(r, 13)
- fn = Split(.[a1].Value)(0)
- End With
- wb.Close False
- For i = 3 To UBound(arr)
- s = arr(i, 2) & "|" & arr(i, 3) & "|" & arr(i, 4)
- If s <> Empty Then
- If Not d.Exists(s) Then
- m = m + 1
- d(s) = m
- brr(m, 1) = m
- For j = 2 To UBound(b)
- brr(m, j) = arr(i, b(j))
- Next
- brr(m, 7) = fn & ":" & arr(i, 12)
- brr(m, c) = arr(i, 10)
- Else
- r = d(s)
- brr(r, 7) = IIf(InStr(brr(r, 7), fn & ":" & arr(i, 12)), brr(r, 7), brr(r, 7) & ";" & fn & ":" & arr(i, 12))
- brr(r, c) = brr(r, c) + arr(i, 10)
- End If
- d1(s & "|" & fn) = d1(s & "|" & fn) + arr(i, 10)
- End If
- Next
- End If
- End If
- Next
- With sh
- .UsedRange.Offset(3).ClearContents
- .[a4].Resize(m, 15) = brr
- .[a4].Resize(m, c).Borders.LineStyle = 1
- arr = .UsedRange
- For i = 4 To UBound(arr)
- Sum = 0
- For j = 8 To c - 1
- s = arr(i, 2) & "|" & arr(i, 3) & "|" & arr(i, 4) & "|" & arr(2, j)
- If s <> Empty Then
- If d1.Exists(s) Then
- arr(i, j) = d1(s)
- Sum = Sum + arr(i, j)
- End If
- End If
- Next
- arr(i, c) = Sum
- Next
- .UsedRange = arr
- ActiveWindow.DisplayZeros = False
- End With
- Application.ScreenUpdating = True
- MsgBox "运行完毕,共用时: " & Format(Timer - tm) & "秒!"
- End Sub
复制代码
|
评分
-
1
查看全部评分
-
|