Excel教程:根据分数为职工评定星级

本文为《别怕,Excel VBA其实特别简单(第3版)》随书问题参考答案

如果H2单元格中保存的成绩是0到150之间的整数,用Select Case解决的代码可以写为:

Sub 用Slect语句评定星级()

Select Case Cells(2, "H").Value

Case Is < 85 Cells(2, "I").Value = "不评级" Case Is < 100 Cells(2, "I").Value = "一星级" Case Is < 115 Cells(2, "I").Value = "二星级" Case Is < 130 Cells(2, "I").Value = "三星级" Case Is < 150 Cells(2, "I").Value = "四星级" Case Else Cells(2, "I").Value = "五星级" End Select End Sub

用If语句解决的代码可以写为:

Sub 用If语句评定星级()

Dim Cj As Integer

Cj = Cells(2, "H").Value

If Cj < 85 Then Cells(2, "I").Value = "不评级" ElseIf Cj < 100 Then Cells(2, "I").Value = "一星级" ElseIf Cj < 115 Then Cells(2, "I").Value = "二星级" ElseIf Cj < 130 Then Cells(2, "I").Value = "三星级" ElseIf Cj < 150 Then Cells(2, "I").Value = "四星级" ElseIf Cj = 150 Then Cells(2, "I").Value = "五星级" End If End Sub

AI助手