我有以下2個代碼,第二個是試圖使用調用函數來調用第一個代碼。Excel VBA調用函數和循環
我有幾個分數範圍(「e42:e48」)。
但是,第二個代碼沒有經過分數列表,它每次只檢查每個分數,直到單擊下一個分數並運行代碼。
非常感謝您的幫助。謝謝。鑫
Sub IfElseIfTest_1()
Dim score As Integer
score = activecell.Value
If score >= 0 And score <= 35 Then
activecell(1, 2).Value = "F"
activecell(1, 2).HorizontalAlignment = xlCenter
activecell(1, 3).Value = "Terrible - needs attention"
ElseIf score >= 36 And score <= 50 Then
activecell(1, 2).Value = "D"
activecell(1, 2).HorizontalAlignment = xlCenter
activecell(1, 3).Value = "Needs attention"
ElseIf score >= 51 And score <= 65 Then
activecell(1, 2).Value = "C"
activecell(1, 2).HorizontalAlignment = xlCenter
activecell(1, 3).Value = "Not bad, could do better"
ElseIf score >= 66 And score <= 80 Then
activecell(1, 2).Value = "B"
activecell(1, 2).HorizontalAlignment = xlCenter
activecell(1, 3).Value = "Good score"
ElseIf score >= 81 And score <= 100 Then
activecell(1, 2).Value = "A"
activecell(1, 2).HorizontalAlignment = xlCenter
activecell(1, 3).Value = "Excellent score, well done!"
Else
MsgBox "Score not valid"
End If
End Sub
Sub IfElseIfTest_1_CallFunction()
Dim score As Range
' need to Dim cell as RANGE, IF AS STRING WILL NOT WORK.
For Each score In Range("e42:e48")
Call IfElseIfTest_1
Next score
End Sub
如果我正確理解你,你想爲每個單元從E42到E48執行'IfElseIfTest_1'。在這種情況下,你有幾個選項,但最簡單的一個可能是在你的第二個Sub – FernAndr