Sub peng() Dim D1 As New Dictionary, D2 As New Dictionary, D3 As New Dictionary Dim i As Long Dim y As Long Dim j As Long Dim dh As Long Dim d3i As Long
Application.ScreenUpdating = False r = Sheet2.[A65536].End(xlUp).Row - 1 arr = Sheet2.Cells(2, 1).Resize(r, 5) t = Timer For i = 1 To r jx = arr(i, 2) jxgz = jx & arr(i, 3) If D2.Exists(jxgz) = False Then D1(jx) = D1(jx) & "," & dh dh = dh + 1 End If If D3.Exists(jxgz & arr(i, 1)) = False Then D2(jxgz) = D2(jxgz) & "," & d3i d3i = d3i + 1 End If D3(jxgz & arr(i, 1)) = D3(jxgz & arr(i, 1)) + 1 Next ReDim arr1(1 To D1.Count + D2.Count, 1 To 11) arr2 = D2.Keys arr3 = D3.Keys arr4 = D3.Items For Each k In D1 xx = 0 ar1 = Split(D1(k), ",") Lk = Len(k) + 1 For i = 1 To UBound(ar1) x = 0 y = y + 1 ar2 = Split(D2(arr2(ar1(i))), ",") lkk = Len(arr2(ar1(i))) + 1 For j = 1 To UBound(ar2) x = x + arr4(ar2(j)) If arr4(ar2(j)) > arr1(y, 11) Then arr1(y, 11) = arr4(ar2(j)) arr1(y, 10) = Mid(arr3(ar2(j)), lkk) End If If arr1(y, 11) > arr1(y, 9) Then s = arr1(y, 9): arr1(y, 9) = arr1(y, 11): arr1(y, 11) = s s = arr1(y, 8): arr1(y, 8) = arr1(y, 10): arr1(y, 10) = s End If If arr1(y, 9) > arr1(y, 7) Then s = arr1(y, 7): arr1(y, 7) = arr1(y, 9): arr1(y, 9) = s s = arr1(y, 6): arr1(y, 6) = arr1(y, 8): arr1(y, 8) = s End If Next j arr1(y, 1) = i arr1(y, 2) = k arr1(y, 3) = Mid(arr2(ar1(i)), Lk) arr1(y, 4) = x xx = xx + x Next i y = y + 1 arr1(y, 1) = i arr1(y, 2) = k arr1(y, 3) = "合计" arr1(y, 4) = xx Next For i = y To 1 Step -1 If arr1(i, 3) = "合计" Then x = arr1(i, 4) arr1(i, 5) = arr1(i, 4) / x Next i Sheet3.[a3].Resize(y, 11) = arr1 MsgBox (Timer - t) Application.ScreenUpdating = True End Sub 根据树形结构原理,用字典做了一个汇总,速度还可以 非常感谢LDY888兄和狼版两位字典高手.都是跟他二位学的.
[此贴子已经被作者于2007-12-3 15:43:57编辑过] |