|
- Sub test1()
-
- Dim ar, br, cr(1 To 100, 1 To 80), dict(1) As Object
- Dim setup, data, results, target As Range
- Dim i As Long, j As Long, x As Long, y As Long
- Dim pos As Long, col As Long, posRow As Long, rowSize As Long, colSize As Long
-
- Sheet3.Activate
- Cells.Clear
-
- Application.ScreenUpdating = False
-
- For j = LBound(dict) To UBound(dict)
- Set dict(j) = CreateObject("Scripting.Dictionary")
- Next
-
- Set target = Range("A2")
-
- ar = Split("分数 排名 划线分 差值")
- For i = 4 To UBound(cr)
- cr(i, 1) = i - 3
- Next
- cr(1, 1) = "班别"
- cr(3, 1) = "排序"
- colSize = 2
-
- setup = Sheet1.Range("A1").CurrentRegion.Value
- For i = 3 To UBound(setup)
- dict(0).Add setup(i, 1), i ' Array(i, Split(setup(i, 2), "—"))
- Next
- For j = 3 To UBound(setup, 2)
- dict(0).Add setup(2, j), j
- cr(2, colSize) = setup(2, j)
- dict(1).Add cr(2, colSize), colSize
- For i = 0 To UBound(ar)
- cr(3, colSize + i) = ar(i)
- Next
- colSize = colSize + 4
- Next
- colSize = colSize - 1
-
- With Sheet2
- data = .Range("A1", .Range("A1").CurrentRegion.Offset(1)).Value
- End With
- QuickSort data, 2, UBound(data) - 1, 1, UBound(data, 2), 2, False '班级名为 数字 形式
-
- pos = 1
- For i = 2 To UBound(data) - 1
- If data(i, 2) <> data(i + 1, 2) Then
- rowSize = 3
- results = cr
- results(1, 2) = data(i, 2)
- posRow = dict(0)(data(pos + 1, 2))
- br = Split(setup(posRow, 2), "—")
- For x = 3 To UBound(setup, 2)
- col = dict(1)(setup(2, x))
- For y = 1 To br(1) - br(0) + 1
- results(y + rowSize, col + 2) = setup(posRow, x)
- Next
- Next
- QuickSort data, pos + 1, i, 1, UBound(data, 2), UBound(data, 2) - 1, True
- For y = pos + br(0) To pos + br(1)
- rowSize = rowSize + 1
- For x = 6 To UBound(data, 2) Step 2
- col = dict(1)(data(1, x))
- results(rowSize, col) = data(y, x)
- results(rowSize, col + 1) = data(y, x + 1)
- If results(rowSize, col) Then results(rowSize, col + 3) = results(rowSize, col) - results(rowSize, col + 2)
- Next
- If data(y, 2) <> data(y + 1, 2) Then Exit For
- Next
- With target
- .Resize(rowSize, colSize).Value = results
- With .CurrentRegion
- Intersect(.Offset(0), .Offset(1)).Borders.LineStyle = xlContinuous
- .HorizontalAlignment = xlCenter
- .Font.Name = "宋体"
- .Rows("1:3").Font.Bold = True
- End With
- For j = 2 To colSize Step 4
- .Offset(1, j - 1).Resize(, UBound(ar) + 1).HorizontalAlignment = xlCenterAcrossSelection
- Next
- End With
- Set target = Cells(Rows.Count, 1).End(xlUp).Offset(2)
- pos = i
- End If
- Next
-
- Set target = Nothing
- For j = LBound(dict) To UBound(dict)
- Set dict(j) = Nothing
- Next
-
- Application.ScreenUpdating = True
- Beep
- End Sub
- Function QuickSort(ar, u As Long, d As Long, l As Long, r As Long, pCol As Long, Optional Flag 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, pCol)
- While t <= b
- If Flag Then 'Order by number DESC
- Do While t < d
- If ar(t, pCol) > pivot Then t = t + 1 Else Exit Do
- Loop
- Do While b > u
- If ar(b, pCol) < pivot Then b = b - 1 Else Exit Do
- Loop
- Else 'Order by number ASC
- Do While t < d
- If ar(t, pCol) < pivot Then t = t + 1 Else Exit Do
- Loop
- Do While b > u
- If ar(b, pCol) > pivot Then b = b - 1 Else Exit Do
- Loop
- 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, pCol, Flag
- If b > u Then QuickSort ar, u, b, l, r, pCol, Flag
- End Function
复制代码 |
|