7楼数组法效果不错,由于成绩总表中班别是有序排列的,可以分别查找第一个和最后一个“四(1)”,这样就可以不用判断了:- Sub cs()
- Dim c As Range, arr, i&, ban$, brr(100, 1 To 2), n&
- Dim crr(1 To 34, 1 To 15), hj As Double, j&, m&, mc&
- ban = Sheets("班档").Range("a2").Value
- With Sheets("成绩总表")
- Set c = .[a:a].Find(ban, , , xlWhole)
- If c Is Nothing Then
- MsgBox "没有查到"
- Exit Sub
- End If
- arr = c.Resize(.[a:a].Find(ban, , , xlWhole, , xlPrevious).Row - c.Row + 1, 5)
- End With
- ' With Sheets("成绩总表")
- ' arr = .Range("a2:e" & .Cells(Rows.Count, 1).End(3).Row).Value
- ' End With
- n = 1
- brr(1, 2) = 0
- For i = 1 To UBound(arr)
- ' If arr(i, 1) = ban Then
- hj = arr(i, 3) + arr(i, 4) + arr(i, 5)
- For j = 1 To n
- If hj > brr(j, 2) Then
- For m = n To j Step -1
- brr(m + 1, 1) = brr(m, 1)
- brr(m + 1, 2) = brr(m, 2)
- Next
- brr(j, 1) = i
- brr(j, 2) = hj
- Exit For
- End If
- Next
- n = n + 1
- ' End If
- Next
- For i = 1 To n - 1
- If i > 34 Then
- x = i - 34
- y = 8
- Else
- x = i
- y = 0
- End If
- If brr(i, 2) <> brr(i - 1, 2) Then mc = i
- crr(x, 1 + y) = mc
- For j = 2 To 5
- crr(x, j + y) = arr(brr(i, 1), j)
- Next
- crr(x, 6 + y) = brr(i, 2)
- Next
- Sheets("班档").Range("a4:o37") = crr
- End Sub
复制代码 |