Difference Between MsgBox 'Cancel' and red cross 'Close' button?

killpaddy

Board Regular
Joined
Jul 31, 2012
Messages
52
Hi,

I have a MsgBox displaying a user selected filepath with OK and Cancel options. IF OK is selected, the code continues. IF Cancel is selected the code loops back for the user to reselect the filepath.

I would like: IF the MsgBox is closed (using the red cross in the top right corner) then 'Exit Sub'.

Below is my current guess using 'vbAbort' for the closing option. However when i click the red cross the code loops back to line 23, as when i click 'Cancel'. How do I achieve what i want?

Thanks

Code:
foldername1 = "C:\\"
23
foldername = GetFolder(foldername1, "Select Filepath")   'lets user select filepath
MsgBoxOpt = MsgBox("Continue using File Path Below?" & vbNewLine & vbNewLine & foldername, vbOKCancel, "FilePath Check")

            If MsgBoxOpt = vbCancel Then
                GoTo 23                
            ElseIf MsgBoxOpt = vbAbort Then
                Exit Sub
            Else
            End If
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
Could you use vbYesNoCancel as below:-
Code:
[COLOR="Navy"]Sub[/COLOR] MG03Sep59
[COLOR="Navy"]Dim[/COLOR] Ans [COLOR="Navy"]As[/COLOR] [COLOR="Navy"]Integer[/COLOR]
Ans = MsgBox("This Message", vbYesNoCancel + vbInformation)
[COLOR="Navy"]Select[/COLOR] [COLOR="Navy"]Case[/COLOR] Ans
    Case vbYes: '[COLOR="Green"][B]Do something for result  "Ok"[/B][/COLOR]
    Case vbNo: '[COLOR="Green"][B]Do Something for result , "No"[/B][/COLOR]
    [COLOR="Navy"]Case[/COLOR] vbCancel: [COLOR="Navy"]Exit[/COLOR] [COLOR="Navy"]Sub[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Select[/COLOR]
[COLOR="Navy"]End[/COLOR] [COLOR="Navy"]Sub[/COLOR]
Regards Mick
 
Upvote 0
Thanks MickG, that is a good solution, suppose I got quite intent on getting the vbOKCancel MsgBox to work for me when I should have taken a step back.

I found (by inserting a break on the 'GoTo 23' line) that pressing the red cross is the equivalent of selecting 'Cancel', i.e. returns the value 'vbCancel' - so excel does not distinguish between these two different commands.

Out of interest and for future reference, can anyone tell me what the 'vbAbort' value is and how it is bought about in this context? As I assumed it would be the 'close' button.
 
Upvote 0
It's what's returned if your messagebox has an Abort button that's pressed. - e.g.:
Code:
msgbox "Choose an option", vbAbortRetryIgnore
 
Upvote 0

Forum statistics

Threads
1,214,423
Messages
6,119,398
Members
448,892
Latest member
amjad24

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