|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test0()
-
- Dim results(), data
- Dim i As Long, j As Long, k As Long
- Dim cnt As Long, pos As Long, colSize As Long
-
- With Worksheets(1).Range("A1").CurrentRegion
- data = Range(.Cells(1), .Offset(1)).Value
- End With
- ReDim results(1 To UBound(data), 1 To 100)
-
- pos = 1
- For j = 1 To 2
- results(pos, j) = data(pos, j)
- Next
- For colSize = j To 5
- results(pos, colSize) = Split("总分 级名次 班名次")(colSize - 3)
- Next
-
- For i = pos + 1 To UBound(data) - 1
- For j = 1 To 2
- results(i, j) = data(i, j)
- Next
- For j = 3 To UBound(data, 2)
- results(i, 3) = results(i, 3) + Val(data(i, j))
- Next
- Next
- For j = 3 To UBound(data, 2)
- results(pos, colSize) = data(pos, j)
- results(pos, colSize + 1) = data(pos, j) & "名次"
- For i = pos + 1 To UBound(data) - 1
- results(i, colSize) = data(i, j)
- Next
- colSize = colSize + 2
- Next
- colSize = colSize - 1
-
- For j = 6 To colSize Step 2
- cnt = 1
- QuickSort results, pos + 1, UBound(results) - 1, 1, colSize, j, True
- results(pos + 1, j + 1) = cnt
- For i = pos + 2 To UBound(results) - 1
- cnt = cnt + 1
- If results(i, j) < results(i - 1, j) Then results(i, j + 1) = cnt Else results(i, j + 1) = results(i - 1, j + 1)
- Next
- Next
-
- j = 3
- cnt = 1
- QuickSort results, pos + 1, UBound(results) - 1, 1, colSize, 3, True
- results(pos + 1, j + 1) = cnt
- For i = pos + 2 To UBound(results) - 1
- cnt = cnt + 1
- If results(i, j) < results(i - 1, j) Then results(i, j + 1) = cnt Else results(i, j + 1) = results(i - 1, j + 1)
- Next
-
- QuickSort results, pos + 1, UBound(results) - 1, 1, colSize, 1, False
- For i = 2 To UBound(results) - 1
- If results(i, 1) <> results(i + 1, 1) Then
- cnt = 1
- QuickSort results, pos + 1, i, 1, colSize, j, True
- results(pos + 1, j + 2) = cnt
- For k = pos + 2 To i
- cnt = cnt + 1
- If results(k, j) < results(k - 1, j) Then results(k, j + 2) = cnt Else results(k, j + 2) = results(k - 1, j + 2)
- Next
- pos = i
- End If
- Next
-
- With Worksheets(2).Range("A1")
- .CurrentRegion.Clear
- With .Resize(UBound(results) - 1, colSize)
- .Borders.LineStyle = xlContinuous
- .HorizontalAlignment = xlCenter
- .Value = results
- End With
- End With
-
- Beep
- End Sub
- Function QuickSort(ar, u As Long, d As Long, l As Long, r As Long, pos As Long, Optional IsDesc As Boolean = True)
- Dim t As Long, b As Long, j As Long, x As Long, pivot, swap
- t = u
- b = d
- pivot = ar((u + d) \ 2, pos)
- While t <= b
- If IsDesc Then
- Do
- If ar(t, pos) > pivot Then t = t + 1 Else Exit Do
- Loop While t < d
- Do
- If ar(b, pos) < pivot Then b = b - 1 Else Exit Do
- Loop While b > u
- Else
- Do
- If ar(t, pos) < pivot Then t = t + 1 Else Exit Do
- Loop While t < d
- Do
- If ar(b, pos) > pivot Then b = b - 1 Else Exit Do
- Loop While b > u
- End If
- If t < b Then
- For x = l To r
- swap = ar(t, x): ar(t, x) = ar(b, x): ar(b, x) = swap
- Next
- t = t + 1: b = b - 1
- Else
- If t = b Then t = t + 1: b = b - 1
- End If
- Wend
- If t < d Then QuickSort ar, t, d, l, r, pos, IsDesc
- If b > u Then QuickSort ar, u, b, l, r, pos, IsDesc
- End Function
复制代码 |
|