楼主 yjzstar |
Q:已知某学校各班级学生的各科成绩,如何快的将所有班级学生各科成绩的前N名或后N名提取出来?
A:利用VBA,通过循环排序和取数能快速的将所有班级学生各科成绩的前N名或后N名提取出来,具体如附件!- Sub dd()
- Dim rng As Range
- Dim rng1 As Range
- Dim n As Integer, n1 As Integer, n2 As Long
- Application.ScreenUpdating = False
- n = InputBox("如需要提取前N名输入2;后N名输入1:")
- n1 = InputBox("请输入需要提取的名次数,如提取前10名则输入10")
- With Sheet3
- .Activate
- n2 = .Cells(Rows.Count, 1).End(xlUp).Row
- Set rng = .Range("A1:L1182")
- End With
- Sheet2.Range("a2:e" & Sheet2.Cells(Rows.Count, 1).End(xlUp).Row).Clear
- For i = 4 To 12
- With Sheet3
- rng.Sort .Range("c1"), 1, .Columns(i), , n, , , xlYes
- Set rng1 = .Range("C2")
- End With
- x = 0
- Do
- If rng1 <> rng1.Offset(-1, 0) Then
- If rng1.Offset(0, i - 3) <> "" Then
- For j = 0 To n1 - 1
- With Sheet2
- .Cells(Rows.Count, 1).End(xlUp).Offset(1, 0) = rng1.Offset(, -2)
- .Cells(Rows.Count, 2).End(xlUp).Offset(1, 0) = rng1.Offset(, -1)
- .Cells(Rows.Count, 3).End(xlUp).Offset(1, 0) = rng1
- .Cells(Rows.Count, 4).End(xlUp).Offset(1, 0) = Sheet3.Range(Cells(1, i), Cells(1, i))
- .Cells(Rows.Count, 5).End(xlUp).Offset(1, 0) = rng1.Offset(0, i - 3)
- End With
- x = x + 1
- Set rng1 = rng1.Offset(1, 0)
- Next
- End If
- End If
- x = x + 1
- Set rng1 = rng1.Offset(1, 0)
- Loop Until x = n2
- Next
- Application.ScreenUpdating = True
- End Sub
有难度,提取数值,请求帮助!.rar |