Office中国论坛/Access中国论坛
标题:
求VBA数据分类汇总代码
[打印本页]
作者:
文件管理
时间:
2017-6-12 15:43
标题:
求VBA数据分类汇总代码
以商品编码为对象,统计每个月的数量和平均单价(相同商品编码的成本金额之和除以数量)。
作者:
文件管理
时间:
2017-6-12 15:44
附件
作者:
roych
时间:
2017-6-14 01:42
用数据透视表,自己处理下。
[attach]61586[/attach]
作者:
周义坤
时间:
2017-6-14 19:31
Sub ek_sky()
Dim ar1 As Variant, ar2 As Variant
Dim ro1 As Object
Dim i As Long, j As Long, k As Long
Set ro1 = CreateObject("scripting.dictionary")
With Sheets("导出数据")
ar1 = .Range("A3:I" & .Cells(.Rows.Count, 1).End(xlUp).Row)
End With
ReDim ar2(1 To UBound(ar1), 1 To 16)
For i = 1 To UBound(ar1)
If Not ro1.exists(ar1(i, 4)) Then
j = j + 1
ro1.Add ar1(i, 4), j
ar2(j, 1) = ar1(i, 4): ar2(j, Month(ar1(i, 1)) + 1) = ar1(i, 8)
ar2(j, 15) = ar1(i, 9): ar2(j, 16) = ar1(i, 8)
Else
ar2(ro1(ar1(i, 4)), Month(ar1(i, 1)) + 1) = ar1(i, 8) + ar2(ro1(ar1(i, 4)), Month(ar1(i, 1)) + 1)
ar2(ro1(ar1(i, 4)), 15) = ar1(i, 9) + ar2(ro1(ar1(i, 4)), 15)
ar2(ro1(ar1(i, 4)), 16) = ar1(i, 9) + ar2(ro1(ar1(i, 4)), 16)
End If
Next i
For k = 1 To j
ar2(k, 14) = Round(ar2(k, 15) / ar2(k, 16), 4)
Next k
Sheets("sheet1").Select
Range("A:O").Clear
Range("A1:O1") = Array("商品编码", "1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月", "平均单价", "总金额")
Range("A2").Resize(j, 15) = ar2
End Sub
复制代码
欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/)
Powered by Discuz! X3.3