|
[广告] VBA代码宝 - VBA编程加强工具 · VBA代码随查随用 · 内置多项VBA编程加强工具 ★ 免费下载 ★ ★ 使用手册★
- Sub 拆分并汇总()
- Dim arr, brr, i, j, r, k, n, d, d1
- Dim sh As Sheets
- Set d = CreateObject("scripting.dictionary")
- Set d1 = CreateObject("scripting.dictionary")
- With Sheets("数据")
- r = .Cells(Rows.Count, 2).End(xlUp).Row
- arr = .Range("a1:f" & r)
- End With
- brr = Sheet4.UsedRange
- ReDim crr(1 To UBound(arr), 1 To 6)
- For i = 2 To UBound(brr)
- sa = brr(i, 2)
- d1(sa) = i
- Next
- For j = 2 To UBound(arr)
- sb = arr(j, 6)
- d(sb) = d(sb) & "," & j
- Next
- Stop
- For Each k In d.keys
- If d1.exists(k) Then
- Sheets("结算表").Copy After:=Sheets(Sheets.Count)
- With ActiveSheet
- .Name = k
- .Cells(6, 1) = k
- For j = 3 To 6
- .Cells(6, j) = brr(d1(k), j)
- Next
- n = 7
- xa = Split(d(k), ",")
- For i = 1 To UBound(xa)
- n = n + 1
- .Cells(n, 1) = n - 7
- For j = 2 To 5
- .Cells(n, j) = arr(xa(i), j)
- Next
- Next
- ra = .[b7].End(xlDown).Row
- Set Rng = Range("b8:g" & ra)
- Rng.Sort [b7], Header:=xlYes
- .Cells(ra + 1, 2) = "总计"
- .Cells(ra + 1, 5) = Application.Sum(Range(Cells(8, 5), Cells(ra, 5)))
- For j = ra To 8 Step -1
- For i = j - 1 To 7 Step -1
- If Cells(j, 2) <> Cells(i, 2) Then
- Rows(j + 1).Insert
- Cells(j + 1, 2) = Cells(j, 2) & " " & "汇总"
- Cells(j + 1, 5) = Application.Sum(Range(Cells(i + 1, 5), Cells(j, 5)))
- j = i
- End If
-
- Next
- Next
- End With
- End If
- Next
- End Sub
复制代码 |
|