初看,不理解为什么排名也要用VBA。
待我继续研究。
十分钟后,看出一点名目了:本示例的意义在于,展示了RANK()函数的实现方法。也就是说,如果你不知道有个排序用的Rank函数的话,你可以重用以下代码进行排序。
不过,不能忘了“Don't invent the wheel”这句编程格言。
Option Explicit
Sub Getsum()
Application.ScreenUpdating = False
'关闭执行程序时发生的屏幕更新现象,加快运行速度
ActiveCell.FormulaR1C1 = "=SUM(RC[-3]:RC[-1])"
'ActiveCell表示当前活动单元格
'FormulaR1C1表示单元格内容,此处等价于Value
'使用相对引用方式,计算当前单元格左偏3格到一格的数值和
Selection.AutoFill Destination:=Range("F2:F10"), Type:=xlFillDefault
'自动填充其它同学的总分
'填充方式为默认填充,即向下填充
End Sub
Sub Arrange()
Dim Inum As Integer '定义学生总数
Dim i As Integer
Inum = 9 '此处设定为9个学生
For i = 2 To Inum
'判断上下两单元格数值是否相等
If (Cells(i + 1, "F").Value = Cells(i, "F").Value) Then
'相等则设置名次单元格数值相等
Cells(i + 1, "G").Value = Cells(i, "G").Value
End If
Next i
End Sub
Sub ArrangeA()
Dim Inum As Integer '定义学生总数
Dim i As Integer
Inum = 9 '此处设定为9个学生
For i = 2 To Inum
'判断上下两单元格数值是否相等
If (Cells(i + 1, "C").Value = Cells(i, "C").Value) Then
'相等则设置名次单元格数值相等
Cells(i + 1, "H").Value = Cells(i, "H").Value
End If
Next i
End Sub
Sub ArrangeB()
Dim Inum As Integer '定义学生总数
Dim i As Integer
Inum = 9 '此处设定为9个学生
For i = 2 To Inum
'判断上下两单元格数值是否相等
If (Cells(i + 1, "D").Value = Cells(i, "D").Value) Then
'相等则设置名次单元格数值相等
Cells(i + 1, "I").Value = Cells(i, "I").Value
End If
Next i
End Sub
Sub ArrangeC()
Dim Inum As Integer '定义学生总数
Dim i As Integer
Inum = 9 '此处设定为9个学生
For i = 2 To Inum
'判断上下两单元格数值是否相等
If (Cells(i + 1, "C").Value = Cells(i, "C").Value) Then
'相等则设置名次单元格数值相等
Cells(i + 1, "H").Value = Cells(i, "H").Value
End If
Next i
End Sub