Msgboxes遵循基于条件语句的两个不同路径

时间:2013-03-27 17:58:53

标签: excel vba

我有一种感觉我再次使事情复杂化了。下面的脚本是我正在尝试做的基础知识。我遇到的问题是msgboxes。

基本上,我需要一个msgbox用于条件B失败(并退出)和一个msgbox以成功完成循环。所以,他们每个人都需要遵循不同的条件,但我不知道该怎么做?

For i = 1 To Val(days)
If hasrun = False Then
    If condition A <> 0 Then do nothing
        ElseIf condition A = 0 Then
            Do this...
    End If
If condition B <> 0 Then
    Exit For
ElseIf condition B  = 0 Then
    Do that…
End If
 Do this other code...   
hasrun = True
Next i
    MsgBox "Script exited because condition B already existed"

    MsgBox "Script finished successfully."
End if

1 个答案:

答案 0 :(得分:2)

您可以使用标志变量记录发现问题的事实:

Dim blnBFail As Boolean     
For i = 1 To Val(days)
If hasrun = False Then
    If condition A <> 0 Then do nothing
        ElseIf condition A = 0 Then
            Do this...
    End If
If condition B <> 0 Then
    blnBFail = True
    Exit For
ElseIf condition B  = 0 Then
    Do that…
End If
 Do this other code...   
hasrun = True
Next i

If blnBFail then
    MsgBox "Script exited because condition B already existed"
Else
    MsgBox "Script finished successfully."
EndIf
End if