|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
代码供参考。。。- 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,12}]
- 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)
- 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) - 1
- brr(m, j) = arr(i, b(j))
- Next
- brr(m, 7) = fn & ":" & arr(i, 12)
- brr(m, 15) = 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, 15) = brr(r, 15) + 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)
- 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)
- End If
- End If
- Next
- Next
- .UsedRange = arr
- ActiveWindow.DisplayZeros = False
- End With
- Application.ScreenUpdating = True
- MsgBox "运行完毕,共用时: " & Format(Timer - tm) & "秒!"
- End Sub
复制代码
|
|