|
按预算项目拆分- Sub ykcbf() '//2024.5.19 分表增加小计、增加总计
- Dim arr, d
- Set d = CreateObject("scripting.dictionary")
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Dim tm: tm = Timer
- Set ws = ThisWorkbook
- Set sh = ws.Sheets(1) '//主表
- bt = 3 '//标题行数
- col = 32 '//拆分字段列号
- col1 = 31 '//分类汇总字段列号
- For Each sht In Sheets
- If sht.Name <> sh.Name Then sht.Delete
- Next
- arr = sh.UsedRange
- For i = bt + 1 To UBound(arr)
- s = arr(i, col): ss = arr(i, col1)
- If Not d.Exists(s) Then Set d(s) = CreateObject("scripting.dictionary")
- If Not d(s).Exists(ss) Then Set d(s)(ss) = CreateObject("scripting.dictionary")
- d(s)(ss)(i) = i
- Next i
- On Error Resume Next
- For Each k In d.keys
- ReDim hj(1 To 2)
- ReDim brr(1 To UBound(arr), 1 To UBound(arr, 2))
- sh.Copy after:=Sheets(Sheets.Count)
- Set sht = Sheets(Sheets.Count)
- m = 0
- With sht
- .Name = k
- .UsedRange.Offset(bt).ClearContents
- .DrawingObjects.Delete
- .Range("d:e,v:v").NumberFormatLocal = "@"
- For Each kk In d(k).keys
- ReDim Sum(1 To 2)
- For Each kkk In d(k)(kk).keys
- m = m + 1
- For j = 1 To UBound(arr, 2)
- brr(m, j) = arr(kkk, j)
- Next
- Sum(1) = Sum(1) + brr(m, 8)
- Sum(2) = Sum(2) + brr(m, 9)
- Next
- m = m + 1
- brr(m, 8) = Sum(1): brr(m, col1) = Split(brr(m - 1, col1), "-")(1) & " 小计"
- brr(m, 9) = Sum(2)
- hj(1) = hj(1) + Sum(1)
- hj(2) = hj(2) + Sum(2)
- Next
- m = m + 1
- brr(m, 8) = hj(1): brr(m, col1) = "总计"
- brr(m, 9) = hj(2)
- .[a4].Resize(m, UBound(arr, 2)) = brr
- r = .Cells(Rows.Count, col).End(3).Row
- .UsedRange.Offset(r + 2).Clear
- .Rows(3).Delete
- End With
- Next k
- sh.Activate
- Set d = Nothing
- Application.DisplayAlerts = True
- Application.ScreenUpdating = True
- MsgBox "共用时:" & Format(Timer - tm) & "秒!"
- End Sub
复制代码
|
|