Message Box When Closing Excel

sn281

New Member
Joined
Dec 13, 2011
Messages
26
Hi all,

Thank you in advance for your help.

I'm okay with VBA but I always get a bit confused with these message boxes when they link.

So what I want to do is to have a message box that pops up when this Excel file is attempted to close that says "Have you uploaded the data?" "Yes" or "No"

If they click "Yes" then Excel closes, if they click "No", it then leads onto the next message box that then says "Would you like to now?".
If they click "Yes" it runs the macro called "gprc", otherwise if they click "No" it then closes Excel.


I think to do this Workbook_BeforeClose will be used, but I just get very confused with the 'if' and 'when' and stuff like that with the dependent message boxes. If someone could help me out or try to explain how to do it I'd really appreciate it, and hopefully I'll learn once and for all from it!

Thank you so much for your help in advance! :) :)
sn281
 
Last edited:

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Hey sn281,

I think this will do the trick:

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If (MsgBox("Have you uploaded the data?", vbYesNo) = vbNo) Then
        If (MsgBox("Would you like to now?", vbYesNo) = vbYes) Then
            gprc
        End If
    End If
    
End Sub
 
Upvote 0
Try

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Have you uploaded the data?", vbYesNo) = vbYes Then Exit Sub
If MsgBox("Would you like to now?", vbYesNo) = vbYes Then Call gprc
End Sub
 
Upvote 0
Hi jaybee3,

Thanks for the quick response. I've put this code into a new module, but when I close the workbook it doesn't actually pop up. It just closes.
I'm on Windows 7, Excel 2010.

Do you know why this is happening? Have I done something stupid? I copied the cose into a new module....

Thanks again,
sn281
 
Upvote 0
Press ALT + F11, in the Project window double click ThisWorkbook then paste in the code.
 
Upvote 0
That works perfectly. Thank you so much. What exactly is the difference between the module, 'ThisWorkbook' and the different sheets in VBA?
Thanks again,
sn281
 
Upvote 0
Try

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
If MsgBox("Have you uploaded the data?", vbYesNo) = vbYes Then Exit Sub
If MsgBox("Would you like to now?", vbYesNo) = vbYes Then Call gprc
End Sub


Would it be possible to have it move thru multiple questions? Like:
Did you save Item 1?
Did you save Item 2?
Did you save Item 3?
If yes to question 3 then excel would close?

Thanks in advance!
 
Upvote 0

Forum statistics

Threads
1,213,554
Messages
6,114,280
Members
448,562
Latest member
Flashbond

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