原帖由
wx_10259334 于 2019-12-12 12:44:51 發(fā)表
第二講 例子.xls已知某班級學生單科成績數(shù)據(jù)(如表),成績?yōu)?00分滿分,其中英文級別和中文級別列數(shù)據(jù)未知,請按如下評判規(guī)則評級并輸出,成績落在[80,100]為A級對應(yīng)優(yōu)秀,成績落在[60,80)為B級對應(yīng)良好,成績落 ...
完整代碼-->
'評判成績級別
Sub JScoreLevel()
'>>>相關(guān)參數(shù)定義>>>
Dim Astr_Score(1 To 20, 1 To 3) As String '成績數(shù)組,數(shù)組大小20*3,Astr_Score(n,1)存儲成績,Astr_Score(n,2) 存儲英文級別,Astr_Score(n,3) 存儲中文級別
Dim int_N As Integer 'Astr_Score數(shù)組行下標
Dim int_R As Integer 'Sheet1表格行號
Dim str_RowDataEndFlag As String 'Sheet1行數(shù)據(jù)結(jié)束標識
Dim int_ScoreCount As Integer '成績個數(shù)
Dim int_Score As Integer '成績
Dim str_EngLevel As String '英文級別
Dim str_ChLevel As String '中文級別
'<<<相關(guān)參數(shù)定義<<<
'>>>從Sheet1讀取成績數(shù)據(jù),存入Astr_Score數(shù)組第一列>>>
int_R = 2 '從第二行開始讀取
str_RowDataEndFlag = Sheet1.Cells(int_R, 1) '讀取第int_r行,第一列數(shù)據(jù)
int_ScoreCount = 0
Do While (str_RowDataEndFlag <> "") 'str_RowDataEndFlag不為空的時候,一直執(zhí)行
int_ScoreCount = int_ScoreCount + 1 '統(tǒng)計成績個數(shù),同時作為Astr_Score的下標
Astr_Score(int_ScoreCount, 1) = Sheet1.Cells(int_R, 2) '讀取第int_r行第2列數(shù)據(jù)(成績)存入Astr_Score第int_ScoreCount行第一列
int_R = int_R + 1 '行號加一行,切換下一行
str_RowDataEndFlag = Sheet1.Cells(int_R, 1) '讀取新一行的第一列數(shù)據(jù)
Loop
'<<<從Sheet1讀取成績數(shù)據(jù),存入Astr_Score數(shù)組第一列<<<
'>>>通過For循環(huán)遍歷Astr_Score數(shù)組第一列數(shù)據(jù),并根據(jù)規(guī)則判斷成績級別>>>
For int_N = 1 To int_ScoreCount
int_Score = Int(Astr_Score(int_N, 1)) '讀取成績數(shù)據(jù),Astr_Score為字符串類型,用int()函數(shù)轉(zhuǎn)換成整型
'用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 '英文級別結(jié)果存入Astr_Score數(shù)組第int_N行第二列
'用select判斷成績中文級別
Select Case str_EngLevel
Case "A"
str_ChLevel = "優(yōu)秀"
Case Is = "B"
str_ChLevel = "良好"
Case "C"
str_ChLevel = "不及格"
Case "D"
str_ChLevel = "異常"
End Select
Astr_Score(int_N, 3) = str_ChLevel '中文級別結(jié)果存入Astr_Score數(shù)組第int_N行第三列
Next int_N
'<<<通過For循環(huán)遍歷Astr_Score數(shù)組第一列數(shù)據(jù),并根據(jù)規(guī)則判斷成績級別<<<
'>>>通過For循環(huán)遍歷Astr_Score數(shù)組第二列和第三列數(shù)據(jù),將英文級別和中文級別數(shù)據(jù)填入Sheet1表格對應(yīng)的列(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第一行數(shù)據(jù)寫到Sheet1第二行,以此類推,Astr_Score第int_N行數(shù)據(jù)寫到Sheet1第int_N+1行,以此類推
Sheet1.Cells(int_R, 3) = str_EngLevel '英文級別數(shù)據(jù)寫入sheet1第int_r行,第3列
Sheet1.Cells(int_R, 4) = str_ChLevel '英文級別數(shù)據(jù)寫入sheet1第int_r行,第4列
Next int_N
'<<<通過For循環(huán)遍歷Astr_Score數(shù)組第二列和第三列數(shù)據(jù),將英文級別和中文級別數(shù)據(jù)填入Sheet1表格對應(yīng)的列(3,4)<<<
End Sub