|
- Sub test1()
- Dim vData, vResult(), vTotal() As Double, i As Long, j As Integer
- Dim rowSize As Long, posRow As Long, iCount As Integer
- vData = Range("E3", Cells(Rows.Count, "A").End(xlUp).Offset(1))
- ReDim vResult(1 To 10000, 1 To UBound(vData, 2)), vTotal(2 To UBound(vData, 2))
- QuickSort vData, 1, UBound(vData) - 1, 1, UBound(vData, 2), 1
- posRow = 5
- vResult(posRow, 1) = "小计"
- For i = 1 To UBound(vData) - 1
- rowSize = rowSize + 1
- iCount = iCount + 1
- For j = 1 To UBound(vData, 2)
- vResult(rowSize, j) = vData(i, j)
- If j > 1 Then
- vResult(posRow, j) = vResult(posRow, j) + vData(i, j)
- vTotal(j) = vTotal(j) + vData(i, j)
- End If
- Next
- If iCount = 3 Then
- rowSize = posRow
- posRow = posRow + 5
- vResult(posRow, 1) = "小计"
- iCount = 0
- Else
- If vData(i, 1) <> vData(i + 1, 1) Then
- rowSize = posRow
- posRow = posRow + 5
- vResult(posRow, 1) = "小计"
- iCount = 0
- End If
- End If
- Next
- vResult(posRow - 4, 1) = "总合计"
- For j = LBound(vTotal) To UBound(vTotal)
- vResult(posRow - 4, j) = vTotal(j)
- Next
- With Range("I3")
- .Resize(Rows.Count - .Row, UBound(vResult, 2)).ClearContents
- .Resize(posRow - 4, UBound(vResult, 2)) = vResult
- End With
- Beep
- End Sub
- Function QuickSort(ar, u As Long, d As Long, l As Long, r As Long, pos As Long)
- Dim up As Long, dn As Long, x As Long, sPivot As String, vSwap
- up = u
- dn = d
- sPivot = ar((u + d) \ 2, pos)
- While up <= dn
- Do While up < d
- If StrComp(ar(up, pos), sPivot, vbTextCompare) = -1 Then up = up + 1 Else Exit Do
- Loop
- Do While dn > u
- If StrComp(sPivot, ar(dn, pos), vbTextCompare) = -1 Then dn = dn - 1 Else Exit Do
- Loop
- If up < dn Then
- For x = l To r
- vSwap = ar(up, x): ar(up, x) = ar(dn, x): ar(dn, x) = vSwap
- Next
- up = up + 1: dn = dn - 1
- Else
- If up = dn Then up = up + 1: dn = dn - 1
- End If
- Wend
- If up < d Then QuickSort ar, up, d, l, r, pos
- If dn > u Then QuickSort ar, u, dn, l, r, pos
- End Function
复制代码 |
|