|
- Sub test()
- Dim d As New Dictionary
- Dim d1 As New Dictionary
- Dim r%, i%
- Dim arr, brr()
- With Worksheets("sheet1")
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- arr = .Range("a2:d" & r)
- m = 1
- For i = 1 To UBound(arr)
- If Not d.Exists(arr(i, 1)) Then
- Set d(arr(i, 1)) = CreateObject("scripting.dictionary")
- End If
- If Not d1.Exists(arr(i, 2)) Then
- m = m + 1
- d1(arr(i, 2)) = m
- End If
- If Not d(arr(i, 1)).Exists(arr(i, 3)) Then
- ReDim brr(1 To 6)
- brr(1) = arr(i, 3)
- Else
- brr = d(arr(i, 1))(arr(i, 3))
- End If
- brr(d1(arr(i, 2))) = brr(d1(arr(i, 2))) + arr(i, 4)
- brr(6) = brr(6) + arr(i, 4)
- d(arr(i, 1))(arr(i, 3)) = brr
- Next
- End With
- With Worksheets("sheet2")
- .Cells.Clear
- s = ""
- .Cells(1, 1) = "仓库"
- .Cells(1, 2) = "货号"
- .Cells(1, 7) = "总计"
- .Cells(1, 3).Resize(1, d1.Count) = d1.Keys
- For Each aa In d.Keys
- brr = Application.Transpose(Application.Transpose(d(aa).Items))
- r = .Cells(.Rows.Count, 2).End(xlUp).Row + 1
- .Cells(r, 2).Resize(UBound(brr), UBound(brr, 2)) = brr
- r1 = .Cells(.Rows.Count, 2).End(xlUp).Row + 1
- .Cells(r1, 2) = "汇总"
- With .Cells(r, 1)
- .Value = aa
- .Resize(r1 - r + 1, 1).Merge
- End With
- .Cells(r1, 3).Resize(1, UBound(brr, 2) - 1).FormulaR1C1 = "=SUM(R[" & r - r1 & "]C:R[-1]C)"
- s = s & "+R" & r1 & "C"
- Next
- r = .Cells(.Rows.Count, 2).End(xlUp).Row + 1
- .Cells(r, 1) = "总计"
- .Cells(r, 3).Resize(1, UBound(brr, 2) - 1).FormulaR1C1 = "=" & Mid(s, 2)
- .Range("a1:g" & r).Borders.LineStyle = xlContinuous
- End With
- End Sub
复制代码 |
|