|
Sub 排考号()
Application.ScreenUpdating = False
Dim ar As Variant
Dim i As Long, r As Long, rs As Long
Dim brr()
Dim d As Object
Set d = CreateObject("scripting.dictionary")
With Sheets("sheet1")
rs = .Cells(Rows.Count, 1).End(xlUp).Row
ar = .Range("a1:i" & rs)
End With
For i = 2 To UBound(ar)
For s = i + 1 To UBound(ar)
If ar(i, 9) > ar(s, 9) Then
For j = 1 To UBound(ar, 2)
k = ar(i, j)
ar(i, j) = ar(s, j)
ar(s, j) = k
Next j
End If
Next s
Next i
For i = 2 To UBound(ar)
If Trim(ar(i, 2)) <> "" Then
d(ar(i, 2)) = d(ar(i, 2)) + 1
End If
Next i
sl = d.Count
ReDim arr(1 To UBound(ar), 1 To UBound(ar, 2) + 1)
With Sheets("排考号")
.[a1].CurrentRegion.Offset(1) = Empty
For i = 2 To UBound(ar) Step sl
m = m + 1
n = 0
ReDim brr(1 To sl, 1 To UBound(ar, 2) + 1)
For s = i To i + sl - 1
If s <= UBound(ar) Then
n = n + 1
For j = 1 To UBound(ar, 2)
brr(n, j) = ar(s, j)
Next j
End If
Next s
If m Mod 2 <> 0 Then
For s = 1 To n
Y = Y + 1
For ss = s + 1 To n
If brr(s, 2) > brr(ss, 2) Then
For j = 1 To UBound(brr, 2)
k = brr(s, j)
brr(s, j) = brr(ss, j)
brr(ss, j) = k
Next j
End If
Next ss
brr(s, UBound(brr, 2)) = Y
Next s
ElseIf m Mod 2 = 0 Then
For s = 1 To n
Y = Y + 1
For ss = s + 1 To n
If brr(s, 2) < brr(ss, 2) Then
For j = 1 To UBound(brr, 2)
k = brr(s, j)
brr(s, j) = brr(ss, j)
brr(ss, j) = k
Next j
End If
Next ss
brr(s, UBound(brr, 2)) = Y
Next s
End If
ws = .Cells(Rows.Count, 1).End(xlUp).Row + 1
.Cells(ws, 1).Resize(n, UBound(brr, 2)) = brr
Next i
End With
MsgBox "ok!"
End Sub
|
|