question about Sub Workbook_BeforeClose

danmen

Board Regular
Joined
Jan 15, 2009
Messages
217
I've written some code to be executed right before I close my workbook however I'm having trouble in the way things get processed

what happens:

1. I close the workbook
2. my coding in Sub Workbook_BeforeClose gets executed
3. Excel asks me if I want to save changes ( yes, no, cancel )

what I'd like to happen:

1. I close the workbook
2. Excel asks me if I want to save changes ( yes, no, cancel )
3. If I select yes or no my coding gets executed and Excel is closed
4. If I select Cancel my coding doesn't get executed and I return to Excel

can anyone show me how to set this up properly?

thx. for any help! =)
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
Like this?

Gary

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
Dim iAnswer As Integer
 
iAnswer = MsgBox("Save changes?", vbYesNoCancel + vbInformation, 
"Save file")
 
Select Case iAnswer
 
    Case vbYes
        'Save the file & close
        ThisWorkbook.Close SaveChanges:=True
    
    Case vbNo
        'Close the file, Do not save
        ThisWorkbook.Close SaveChanges:=False
    
    Case vbCancel
        MsgBox "Command aborted!"
        Exit Sub
    
End Select
 
End Sub
 
Upvote 0
Try something like this...

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    Dim Reply As Long
    
    Reply = MsgBox("Do you want to save changes?", vbYesNoCancel, "Save Changes")
    
    Select Case Reply
        Case vbYes
            'Save changes here
            Call MyCode
            ThisWorkbook.Save
        Case vbNo
            'Don't save changes and exit
            Call MyCode
            ThisWorkbook.Saved = True
        Case vbCancel
            'Don't exit
            Cancel = True
    End Select
    
End Sub

Private Sub MyCode()
    ' YOUR CODE HERE
    ' This code doesn't save changes
End Sub

EDIT: I'm a day late and a dollar short again I see.
 
Upvote 0
Alternative, slightly shorter version:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim ans As Integer
ans = MsgBox("Do you want to save changes?", vbYesNoCancel)
Select Case ans
    Case vbCancel
        Cancel = True
        Exit Sub
    Case Else
End Select
'Put your code here
If ans = vbYes Then Me.Save
End Sub
 
Upvote 0
thx. guys ... I tried AlphaFrog's version first and it works great (!) now supposing I'd like the worksheet name to appear ie:



instead of:

Reply = MsgBox("Do you want to save changes?", vbYesNoCancel, "Save Changes")


I want:

Reply = MsgBox("Do you want to save changes to WORKSHEET NAME HERE?", vbYesNoCancel, "Save Changes")



how would the coding of the second line look like then? thx. again to all!


OK I FIGURED IT OUT (LOL)

Reply = MsgBox("Do you want to save changes to " & ThisWorkbook.Name & "?", vbYesNoCancel, "Save Changes")
 
Last edited:
Upvote 0
Reply = MsgBox("Do you want to save changes to " & Thisworkbook.Name & "?", vbYesNoCancel, "Save Changes")
 
Upvote 0

Forum statistics

Threads
1,224,612
Messages
6,179,890
Members
452,948
Latest member
Dupuhini

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