笨办法,Consolidate加数组,速度有些慢,用62楼数据18秒。请高手指正。 Sub 汇总1() Application.ScreenUpdating = 0 Dim arr, arr1, arr2, arr3, arr4 t = Timer sj = "数据" bb = "报表" ls = "临时" Sheets(bb).Select Set NewSheet = Worksheets.Add NewSheet.Name = ls Sheets(sj).Select n = [a65536].End(xlUp).Row ReDim arr(1 To n, 1 To 5) For i = 1 To n arr(i, 1) = Trim(Cells(i, 1)) arr(i, 2) = Trim(Cells(i, 2)) arr(i, 3) = Trim(Cells(i, 3)) If i = 1 Then arr(i, 4) = "数量" arr(i, 5) = arr(i, 2) & arr(i, 3) Else: arr(i, 4) = 1 arr(i, 5) = arr(i, 2) & "-" & arr(i, 3) End If Next i Sheets(ls).Select Range("a1:a" & n) = Application.Index(arr, , 5) Range("b1:b" & n) = Application.Index(arr, , 4) s = ls & "!R1C1:R" & n & "C2" Sheets(ls).[c1].Consolidate s, xlSum, 1, 1 x = [c65536].End(xlUp).Row arr1 = Range("c2:d" & x) Range("a:d").Delete ReDim jxgz(1 To UBound(arr1), 1 To 3) For i = 1 To UBound(arr1) jxgz(i, 1) = Split(arr1(i, 1), "-")(0) jxgz(i, 2) = Split(arr1(i, 1), "-")(1) jxgz(i, 3) = arr1(i, 2) Next i Range("a2:a" & UBound(jxgz) + 1) = Application.Index(jxgz, , 1) Range("a1") = "机型" x = [a1].End(xlDown).Row Range("a1:a" & x).AdvancedFilter 2, , [b1], 1 y = [b1].End(xlDown).Row Range("c2:c" & y).FormulaR1C1 = "=COUNTIF(R2C1:R" & x & "C1,RC[-1])" arr2 = Range("b2:c" & y) gzzl = Application.Sum(Range("c2:c" & y)) Range("a:d").Delete ReDim hz(1 To gzzl + y - 1, 1 To 11) For i1 = 1 To UBound(arr2) x = arr2(i1, 1) y = arr2(i1, 2) ReDim arr3(1 To y, 1 To 2) k = 0 g = 0 For i2 = 1 To UBound(jxgz) If jxgz(i2, 1) = x Then k = k + 1 arr3(k, 1) = jxgz(i2, 2) arr3(k, 2) = jxgz(i2, 3) g = g + jxgz(i2, 3) End If Next i2 For i3 = 1 To UBound(arr3) + 1 l = l + 1 hz(l, 1) = i3 hz(l, 2) = x If i3 <= UBound(arr3) Then a = arr3(i3, 1) hz(l, 3) = a b = arr3(i3, 2) hz(l, 4) = b hz(l, 5) = Round(b / g, 4) ReDim arr4(1 To b, 1 To 2) k = 0 For i4 = 2 To UBound(arr) Step 1 If arr(i4, 5) = x & "-" & a Then k = k + 1 arr4(k, 1) = arr(i4, 1) arr4(k, 2) = arr(i4, 4) End If Next i4 Range("a1") = arr(1, 1) Range("b1") = arr(1, 4) Range("a2:a" & b + 1) = Application.Index(arr4, , 1) Range("b2:b" & b + 1) = Application.Index(arr4, , 2) s = ls & "!R1C1:R" & b + 1 & "C2" Sheets(ls).[c1].Consolidate s, xlSum, 1, 1 m = [c65536].End(xlUp).Row Range("c1:d1").AutoFilter Range("c1:d" & m).Sort Key1:=Range("d1"), Order1:=xlDescending, Header:= _ xlGuess, OrderCustom:=1, MatchCase:=False, Orientation:=xlTopToBottom, _ SortMethod:=xlPinYin, DataOption1:=xlSortNormal hz(l, 6) = Range("c2").Value hz(l, 7) = Range("d2").Value hz(l, 8) = Range("c3").Value hz(l, 9) = Range("d3").Value hz(l, 10) = Range("c4").Value hz(l, 11) = Range("d4").Value Range("a:d").Delete Else: hz(l, 3) = "合计" hz(l, 4) = g hz(l, 5) = 1 End If Next i3 Next i1 Application.DisplayAlerts = False Sheets(ls).Delete Application.DisplayAlerts = True Sheets(bb).Select Range("a3:k" & UBound(hz) + 2) = hz For i = 3 To UBound(hz) + 2 Step 1 If Cells(i, 3) = "合计" Then Range("a" & i & ":k" & i).Interior.ColorIndex = 43 End If Next Application.ScreenUpdating = 1 Range("l2") = Timer - t End Sub
|