- Sub ykcbf() '//2022.12.2
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Dim arr, brr, d
- Set d = CreateObject("Scripting.Dictionary")
- Set sh = Sheet1
- Dim tm: tm = Timer
- On Error Resume Next
- Set mb = Sheets("模板")
- For Each sht In Sheets
- If InStr(sht.Name, "汇总") = 0 And InStr(sht.Name, "模板") = 0 Then sht.Delete
- Next sht
- With sh
- r = .Cells(.Rows.Count, "b").End(xlUp).Row
- arr = .Range("a2:g" & r)
- End With
- ReDim brr(1 To UBound(arr), 1 To 11)
- For i = 1 To UBound(arr)
- s = arr(i, 3) & "|" & arr(i, 4)
- ss = arr(i, 1) & "|" & arr(i, 2)
- If Not d.exists(s) Then
- Set d(s) = CreateObject("Scripting.Dictionary")
- End If
- d(s)(ss) = Array(arr(i, 5), arr(i, 6), arr(i, 7))
- Next
- For Each aa In d.keys
- m = 0
- a = Split(aa, "|")
- Set sht = Sheets.Add(after:=Sheets(Sheets.Count))
- sht.Name = a(0)
- mb.Cells.Copy
- sht.Cells.Select
- ActiveSheet.Paste
- With sht
- .[a10:n1000].Clear
- .[d4] = a(0)
- .[k5] = a(1)
- For Each bb In d(aa).keys
- m = m + 1
- b = Split(bb, "|")
- brr(m, 1) = b(0)
- brr(m, 2) = b(1)
- brr(m, 3) = d(aa)(bb)(0)
- brr(m, 5) = d(aa)(bb)(1)
- brr(m, 8) = d(aa)(bb)(2)
- rc = rc + brr(m, 5)
- cc = cc + brr(m, 8)
- Next
- .[a10].Resize(m, 10) = brr
- .[a10].Resize(m, 14).Borders.LineStyle = 1
- Dim hj(1 To 2)
- .[a10].Resize(m, 10).Sort .[a10], 1
- i = 10
- Do
- If .Cells(i, 1) = .Cells(i + 1, 1) Then
- hj(1) = hj(1) + .Cells(i, 5)
- hj(2) = hj(2) + .Cells(i, 8)
- .Cells(i, 3).Resize(1, 2).Merge
- i = i + 1
- Else
- hj(1) = hj(1) + .Cells(i, 5)
- hj(2) = hj(2) + .Cells(i, 8)
- .Cells(i, 3).Resize(1, 2).Merge
- i = i + 1
- .Cells(i, 1).EntireRow.Insert '//插入小计
- .Cells(i, 3) = "本月合计"
- .Cells(i, 3).Resize(1, 2).Merge
- .Cells(i, 5) = hj(1)
- .Cells(i, 8) = hj(2)
- If .Cells(i, 5) = .Cells(i, 8) Then .Cells(i, 11) = "平"
- i = i + 1
- hj(1) = 0: hj(2) = 0
- End If
- Loop While .Cells(i, 1) <> ""
- r = .Cells(.Rows.Count, 3).End(xlUp).Row + 1
- .Cells(r, 3) = "本年累计"
- .Cells(i, 3).Resize(1, 2).Merge
- .Cells(r, 5) = rc
- .Cells(r, 8) = cc
- .[a10].Resize(r - 9, 14).Borders.LineStyle = 1
- End With
- Next
- sh.Activate
- Application.ScreenUpdating = True
- MsgBox "拆分完毕,共用时: " & Format(Timer - tm, "0.000秒"), , "提示"
- End Sub
复制代码
|