|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
Sub test()
Dim d As New Dictionary
Dim r%, i%
Dim arr, brr
With Worksheets("数据")
r = .Cells(.Rows.Count, 2).End(xlUp).Row
arr = .Range("b4:u" & r)
For i = 1 To UBound(arr)
If Not d.Exists(arr(i, 13)) Then
Set d(arr(i, 13)) = CreateObject("scripting.dictionary")
End If
xm = arr(i, 5) & "+" & arr(i, 7)
If Not d(arr(i, 13)).Exists(xm) Then
ReDim brr(1 To 19)
For j = 1 To 5 '单元格填入到第几列
brr(j) = arr(i, j + 2)
brr(7) = arr(i, 10) / arr(i, 8)
Next
Else
brr = d(arr(i, 13))(xm)
End If
For j = 6 To 8 Step 2
brr(j) = brr(j) + arr(i, j + 2)
Next
d(arr(i, 13))(xm) = brr
Next
m = 0
With Worksheets("测试")
.Range("4:" & .Rows.Count).Clear
ss = ""
For Each aa In d.Keys
m = m + 1
r = .Cells(.Rows.Count, 4).End(xlUp).Row + 1
.Cells(r, 3) = aa
r0 = r
For Each bb In d(aa).Keys
brr = d(aa)(bb)
.Cells(r, 4).Resize(1, 8) = brr '单元格填入到第几列
r = r + 1
Next
.Cells(r, 4) = "小计"
.Cells(r, 9).FormulaR1C1 = "=SUM(R[" & r0 - r & "]C:R[-1]C)"
.Cells(r, 11).FormulaR1C1 = "=SUM(R[" & r0 - r & "]C:R[-1]C)"
ss = ss & "+R" & r & "C"
.Cells(r0, 2) = m
.Cells(r0, 2).Resize(r - r0 + 1, 1).Merge
.Cells(r0, 3).Resize(r - r0 + 1, 1).Merge
Next
r = .Cells(.Rows.Count, 4).End(xlUp).Row
.Cells(r + 1, 3) = "总计"
.Cells(r + 1, 9).FormulaR1C1 = "=" & Mid(ss, 2)
.Cells(r + 1, 11).FormulaR1C1 = "=" & Mid(ss, 2)
With .Range("b3:k" & r + 1)
.Borders.LineStyle = xlContinuous
.HorizontalAlignment = xlCenter
.VerticalAlignment = xlCenter
End With
End With
End With
End Sub
|
|