stuartmacdonald

New Member
Joined
May 26, 2009
Messages
48
I'm looking to give users an option when closing a workbook and have put in the following VBA code, but I cant figure out how to trigger the opening of another workbook if 'yes' is selected.


Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)    If MsgBox("Do you need to refresh timesheets?", _
            vbQuestion + vbYesNo) = vbNo Then
        Cancel = True
    End If
End Sub

I need to add in an, if yes


Workbooks.Open "C:\Users\Stuart MacDonald\OneDrive - Cles\Order book\Refresh.xlsm"
 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Try something like this:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)    
    If MsgBox("Do you need to refresh timesheets?", _
            vbQuestion + vbYesNo) = vbNo
        Cancel = True
    Else
        Workbooks.Open "C:\Users\Stuart MacDonald\OneDrive - Cles\Order book\Refresh.xlsm"
    End If
End Sub
or
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)    
    Select Case MsgBox("Do you need to refresh timesheets?", _
            vbQuestion + vbYesNo) = vbNo Then
        Case vbYes
            Workbooks.Open "C:\Users\Stuart MacDonald\OneDrive - Cles\Order book\Refresh.xlsm"
        Case vbNo
            Cancel = True
        Case Else
            'what to do if they do not select Yes or No
    End Select
End Sub
 
Last edited:
Upvote 0
The first version works, it opens the workbook ut it's not closing the original file when you press either Yes or No.....
I had a minor typo in the second code (forgot to remove the word "Then").
The first code won't close it when you press "No" because you have "Cancel = True", which means to cancel the Close command (I was using the safe logic you used originally).

It sounds like maybe you want it to close no matter what they choose, just if they choose "Yes", you want the other file opened. If that is the case, try this:
Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
    If MsgBox("Do you need to refresh timesheets?", _
            vbQuestion + vbYesNo) = vbYes Then
        Workbooks.Open "C:\Users\Stuart MacDonald\OneDrive - Cles\Order book\Refresh.xlsm"
    End If
End Sub
 
Upvote 0
Correct - I want it to close no matter what they choose, just if they choose "Yes", the other file opened.

The code above closes the workbook if you choose 'no', but does not if you choose 'yes'

Whats missing?
 
Upvote 0
Did you copy and paste the code I posted in my last post, verbatim?
It works for me. Regardless of whether I select "Yes" or "No", the workbook that this macro is contained in will close.
 
Upvote 0

Forum statistics

Threads
1,214,385
Messages
6,119,209
Members
448,874
Latest member
b1step2far

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