user65795
user65795

Reputation: 37

Why does this vba MsgBox always return zero?

I have a VBA MsgBox with vbYesNo as the button-type but no matter what button is pushed it always shows a return result as zero. Sorry this isn't the entire code but the below code is a portion of the code that is giving me trouble.

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

Basically what I'm trying to do is write this loop without using the standard phrase if x=vbNo then Exit Sub. I'd rather try to let it run it's course. But that would only work if MsgBox could change the value of RunThis to False.

Any help is really appreciated.

Upvotes: 1

Views: 819

Answers (2)

Taylor Scafe
Taylor Scafe

Reputation: 45

I would place the MsgBox in an if statement. Then based on the result do an action

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

Upvotes: 0

Bond
Bond

Reputation: 16311

= is used both as an assignment operator and a comparison operator in VBA. You can't assign and compare in a single statement (at least not without the unexpected results you're getting). Separate the assignment from the comparison:

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

You could even remove the need for strCheckFolders altogether:

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

Upvotes: 3

Related Questions