为什么这个vba MsgBox总是返回零?

时间:2015-08-18 22:02:21

标签: vba

我有一个VBA MsgBoxvbYesNo作为按钮类型,但无论按下什么按钮,它总是显示返回结果为零。对不起,这不是整个代码,但下面的代码是代码的一部分,给我带来了麻烦。

Sub testMsg()

Dim strCheckFolders As Integer ' <== vbyesno MsgBoxResult (7 = no, 6 = yes)
Dim RunThis As Boolean

RunThis = True

Do Until RunThis = False
    If strCheckFolders = MsgBox("Do you want to check folders for reports to print?", _
    vbYesNo, "Check for Reports to Print") = vbNo Then RunThis = False
Loop

End Sub

基本上我要做的就是在不使用标准短语if x=vbNo then Exit Sub的情况下编写此循环。我宁愿尝试让它运行它的过程。但只有在MsgBox可以将RunThis的值更改为False时,这才有效。

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

=既可用作VBA中的赋值运算符,也可用作比较运算符。您无法在单个陈述中进行分配和比较(至少在没有您获得的意外结果的情况下)。将作业与比较分开:

Do Until RunThis = False
    strCheckFolders = MsgBox("Do you want to check folders for reports to print?", vbYesNo, "Check for Reports to Print")
    If strCheckFolders = vbNo Then RunThis = False
Loop

您甚至可以完全取消对strCheckFolders的需求:

Do Until RunThis = False
    If MsgBox("Do you want to check folders for reports to print?", vbYesNo, _
        "Check for Reports to Print") = vbNo Then RunThis = False
Loop

答案 1 :(得分:0)

我会将MsgBox放在if语句中。然后根据结果做一个动作

Do Until RunThis = False
If MsgBox("Do you want to check folders for reports to print?", vbYesNo) = vbNo Then
    RunThis = False
End If
Loop
End Sub