|
[广告] Excel易用宝 - 提升Excel的操作效率 · Excel / WPS表格插件 ★ 免费下载 ★ ★ 使用帮助★
- Sub test()
- Dim r%, i%
- Dim arr, brr(), crr
- Dim d As Object
- Application.ScreenUpdating = False
- Application.DisplayAlerts = False
- Set d = CreateObject("scripting.dictionary")
- mc = Application.InputBox(prompt:="请输入需要排名前N名:", Title:="操作提示", Default:=5, Type:=1)
- With Worksheets("文科")
- .AutoFilterMode = False
- r = .Cells(.Rows.Count, 1).End(xlUp).Row
- c = .Cells(2, .Columns.Count).End(xlToLeft).Column
- arr = .Range("a2").Resize(r - 1, c)
- End With
- For j = 5 To UBound(arr, 2)
- If arr(1, j) <> "年排" And arr(1, j) <> "班排" Then
- m = 0
- For i = 2 To UBound(arr)
- If arr(i, j + 1) <= mc Then
- m = m + 1
- ReDim Preserve brr(1 To 3, 1 To m)
- brr(1, m) = arr(i, j)
- brr(2, m) = arr(i, 1)
- brr(3, m) = arr(i, 2)
- End If
- Next
- If m > 0 Then
- ReDim crr(1 To UBound(brr, 2), 1 To UBound(brr))
- For x = 1 To UBound(brr)
- For y = 1 To UBound(brr, 2)
- crr(y, x) = brr(x, y)
- Next
- Next
- d(arr(1, j)) = crr
- End If
- End If
- Next
- ls = d.Count * 3
- With Worksheets("文科前10名")
- .Cells.Clear
- With .Range("a1")
- .Value = "文科年级各科前" & mc & "名"
- .Resize(1, ls).Merge
- With .Font
- .Name = "微软雅黑"
- .Size = 16
- End With
- End With
- n = 1
- For Each aa In d.keys
- crr = d(aa)
- With .Cells(2, n)
- .Value = aa
- .Resize(1, 3).Merge
- End With
- .Cells(3, n).Resize(1, 3) = Array("成绩", "姓名", "班级")
- .Cells(4, n).Resize(UBound(crr), UBound(crr, 2)) = crr
- .Cells(4, n).Resize(UBound(crr), UBound(crr, 2)).Sort key1:=.Cells(4, n), order1:=xlDescending, Header:=xlNo
- With .Cells(2, n).Resize(2 + UBound(crr), UBound(crr, 2))
- .Borders.LineStyle = xlContinuous
- With .Font
- .Name = "微软雅黑"
- .Size = 11
- End With
- End With
- n = n + 3
- Next
- .Columns(1).Resize(, ls).AutoFit
- With .UsedRange
- .HorizontalAlignment = xlCenter
- .VerticalAlignment = xlCenter
- End With
- End With
- End Sub
复制代码 |
|