MsgBox Query

Skyybot

Well-known Member
Joined
Feb 18, 2023
Messages
875
Office Version
  1. 365
Platform
  1. Windows
Novice here. I have a question regarding MsgBox return values. Please keep in mind that the following example will not be used in "Real Life". I only want to satisfy my curiosity. Why does the following code never get to the "Else" line?

Sub curious()

If MsgBox("Hello", vbOKOnly, "Test") = vbOK Then
Debug.Print "Yes was clicked!"
Else
Debug.Print "Close button (red X) was clicked."
End If

End Sub
 

Excel Facts

Waterfall charts in Excel?
Office 365 customers have access to Waterfall charts since late 2016. They were added to Excel 2019.
Hi,
your msgbox is just displaying button OK - pressing the X returns the same value (1) as the OK button.

try this example

VBA Code:
Sub curious()

If MsgBox("Hello", vbYesNo, "Test") = vbYes Then
    Debug.Print "Yes was clicked!"
Else
    Debug.Print "No was clicked."
End If

End Sub

You can read more in VBA helpfile: MsgBox function (Visual Basic for Applications)
 
Upvote 0
Hi,
your msgbox is just displaying button OK - pressing the X returns the same value (1) as the OK button.

try this example

VBA Code:
Sub curious()

If MsgBox("Hello", vbYesNo, "Test") = vbYes Then
    Debug.Print "Yes was clicked!"
Else
    Debug.Print "No was clicked."
End If

End Sub

You can read more in VBA helpfile: MsgBox function (Visual Basic for Applications)
Thank you for the reply but I'm not trying to make a functional subroutine here. I just want to know why the Close button has the same value as the Ok button. ;)
 
Upvote 0
I just want to know why the Close button has the same value as the Ok button. ;)
you are performing the same action to dismissing the Msgbox - both actions return the value of 1.

Dave
 
Upvote 0
Solution

Forum statistics

Threads
1,214,861
Messages
6,121,973
Members
449,059
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top