原帖由
wx_10259334 于 2019-12-12 12:44:51 发表

第二讲 例子.xls已知某班级学生单科成绩数据(如表),成绩为100分满分,其中英文级别和中文级别列数据未知,请按如下评判规则评级并输出,成绩落在[80,100]为A级对应优秀,成绩落在[60,80)为B级对应良好,成绩落 ...
完整代码-->
'评判成绩级别
Sub JScoreLevel()
'>>>相关参数定义>>>
Dim Astr_Score(1 To 20, 1 To 3) As String '成绩数组,数组大小20*3,Astr_Score(n,1)存储成绩,Astr_Score(n,2) 存储英文级别,Astr_Score(n,3) 存储中文级别
Dim int_N As Integer 'Astr_Score数组行下标
Dim int_R As Integer 'Sheet1表格行号
Dim str_RowDataEndFlag As String 'Sheet1行数据结束标识
Dim int_ScoreCount As Integer '成绩个数
Dim int_Score As Integer '成绩
Dim str_EngLevel As String '英文级别
Dim str_ChLevel As String '中文级别
'<<<相关参数定义<<<
'>>>从Sheet1读取成绩数据,存入Astr_Score数组第一列>>>
int_R = 2 '从第二行开始读取
str_RowDataEndFlag = Sheet1.Cells(int_R, 1) '读取第int_r行,第一列数据
int_ScoreCount = 0
Do While (str_RowDataEndFlag <> "") 'str_RowDataEndFlag不为空的时候,一直执行
int_ScoreCount = int_ScoreCount + 1 '统计成绩个数,同时作为Astr_Score的下标
Astr_Score(int_ScoreCount, 1) = Sheet1.Cells(int_R, 2) '读取第int_r行第2列数据(成绩)存入Astr_Score第int_ScoreCount行第一列
int_R = int_R + 1 '行号加一行,切换下一行
str_RowDataEndFlag = Sheet1.Cells(int_R, 1) '读取新一行的第一列数据
Loop
'<<<从Sheet1读取成绩数据,存入Astr_Score数组第一列<<<
'>>>通过For循环遍历Astr_Score数组第一列数据,并根据规则判断成绩级别>>>
For int_N = 1 To int_ScoreCount
int_Score = Int(Astr_Score(int_N, 1)) '读取成绩数据,Astr_Score为字符串类型,用int()函数转换成整型
'用if判断成绩英文级别
If int_Score >= 80 And int_Score <= 100 Then
str_EngLevel = "A"
ElseIf int_Score >= 60 And int_Score < 80 Then
str_EngLevel = "B"
ElseIf int_Score > 0 And int_Score < 60 Then
str_EngLevel = "C"
Else
str_EngLevel = "D"
End If
Astr_Score(int_N, 2) = str_EngLevel '英文级别结果存入Astr_Score数组第int_N行第二列
'用select判断成绩中文级别
Select Case str_EngLevel
Case "A"
str_ChLevel = "优秀"
Case Is = "B"
str_ChLevel = "良好"
Case "C"
str_ChLevel = "不及格"
Case "D"
str_ChLevel = "异常"
End Select
Astr_Score(int_N, 3) = str_ChLevel '中文级别结果存入Astr_Score数组第int_N行第三列
Next int_N
'<<<通过For循环遍历Astr_Score数组第一列数据,并根据规则判断成绩级别<<<
'>>>通过For循环遍历Astr_Score数组第二列和第三列数据,将英文级别和中文级别数据填入Sheet1表格对应的列(3,4)>>>
For int_N = 1 To int_ScoreCount
str_EngLevel = Astr_Score(int_N, 2) '读取英文级别
str_ChLevel = Astr_Score(int_N, 3) '读取中文级别
int_R = int_N + 1 'Astr_Score第一行数据写到Sheet1第二行,以此类推,Astr_Score第int_N行数据写到Sheet1第int_N+1行,以此类推
Sheet1.Cells(int_R, 3) = str_EngLevel '英文级别数据写入sheet1第int_r行,第3列
Sheet1.Cells(int_R, 4) = str_ChLevel '英文级别数据写入sheet1第int_r行,第4列
Next int_N
'<<<通过For循环遍历Astr_Score数组第二列和第三列数据,将英文级别和中文级别数据填入Sheet1表格对应的列(3,4)<<<
End Sub