先处理,最后在排序 一点也不比前面的差,优化一下,还能更快 Sub bbb2() Dim D1 As New Dictionary, D2 As New Dictionary Dim dr1 As New Dictionary, dr2 As New Dictionary, dr3 As New Dictionary Dim xh$, gz$, xhgz$, sl&, zl&, zzl& Dim ar() t = Timer Application.ScreenUpdating = False dr2.CompareMode = TextCompare dr3.CompareMode = TextCompare D1.CompareMode = TextCompare D2.CompareMode = TextCompare
r = Sheet2.[A65536].End(xlUp).Row - 1 arr = Sheet2.Cells(2, 1).Resize(r, 4) For i = 1 To r If dr1.Exists(arr(i, 1)) = False Then dr1(arr(i, 1)) = dr1.Count + 1 s = dr2(arr(i, 2)) If dr3.Exists(arr(i, 2) & arr(i, 3)) = False Then dr3(arr(i, 2) & arr(i, 3)) = dr3.Count + 1 Next tx = dr2.Count + dr3.Count ReDim ar(1 To tx, 1 To 11) ReDim ar2(1 To tx, 1 To dr1.Count)
For i = 1 To r xh = arr(i, 2) gz = arr(i, 3) xhgz = xh & gz D2(xh) = D2(xh) + 1 '''''''''''''''''''''''''' 几种型号 每种型号有几台 If Not D1.Exists(xhgz) Then ii = ii + 1 '''''''''''''''''''''''''''''''在报表中的行数 D1(xhgz) = ii ar(ii, 2) = xh ar(ii, 3) = gz End If rr = D1(xhgz) yy = dr1(arr(i, 1)) ar(rr, 4) = ar(rr, 4) + 1 ar2(rr, yy) = ar2(rr, yy) + 1 Next i Debug.Print "打包时间" & Timer - t
' tt = Timer trr = dr1.Items krr = dr1.Keys For i = 1 To ii ar(i, 5) = ar(i, 4) / D2(ar(i, 2)) For jj = 0 To UBound(trr) sl = ar2(i, trr(jj)) If sl > 0 Then If sl >= ar(i, 7) Then ' 前三名,这是最笨的方法 ar(i, 10) = ar(i, 8): ar(i, 11) = ar(i, 9) ar(i, 8) = ar(i, 6): ar(i, 9) = ar(i, 7) ar(i, 6) = krr(jj): ar(i, 7) = sl ElseIf sl >= ar(i, 9) Then ar(i, 10) = ar(i, 8): ar(i, 11) = ar(i, 9) ar(i, 8) = krr(jj): ar(i, 9) = sl ElseIf sl > ar(i, 11) Then ar(i, 10) = krr(jj): ar(i, 11) = sl End If End If Next Next For i = ii + 1 To tx nm = dr2.Keys(i - ii - 1) ar(i, 2) = nm & String(20, " ") ar(i, 3) = "合计" ar(i, 4) = D2(nm) ar(i, 5) = "100%" Next Cells(ii + 3, 1).Resize(tx - ii, 11).Interior.ColorIndex = 43 Range("a3").Resize(tx, 11) = ar Range("a3").Resize(tx, 11).sort Key1:=Range("B3"), Key2:=Range("D3"), Order2:=xlDescending ', Order2:=xlAscending ' ii = 0 arr = Range("a3").Resize(tx, 3) For i = 1 To tx ii = ii + 1 arr(i, 2) = Replace(arr(i, 2), " ", "") arr(i, 1) = ii If arr(i, 3) = "合计" Then ii = 0 Next Range("a3").Resize(tx, 3) = arr [l1] = Timer - t Application.ScreenUpdating = True End Sub |