• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

I'm looking for a Macro to issue a warning when closing an excel workbook.

I have a workbook which I share with other team members. To ensure that they do not let things go awry for otehr colleagues I want a macro that issues a warning when the workbook is closed. I want the warning to read "Ensure the data matches V1 prior to closing this file." Is this possible and if so how.

I have little understanding of macros at all other than that they can be used to do lots of useful things.

Thank you
 
Code:
Option Explicit

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Res As VbMsgBoxResult

    Res = MsgBox("Ensure the data matches V1 prior to closing this file." & vbNewLine & vbNewLine & "OK - CLOSE       CANCEL - OPEN", vbQuestion + vbOKCancel, "Check V1 Data")
    
    If Res = vbOK Then
        Cancel = False
        Exit Sub
    End If
    
    Cancel = True
   
End Sub
 

Attachments

  • MsgBox Close Workbook.xlsb
    13.9 KB · Views: 4
Thanks for this but I'm not sure what to do with it. I tried copying and pasting it into the View Code bit of the worksheet but there waasn't any warning offered when I closed the file. It's likely to be me rather than the code no doubt.
 
The macro is to be pasted into the ThisWorkbook Module.;

Download the sample workbook and open the VBA coding window. Easiest way is to right click any sheet tab and select VIEW CODE.
On the left side find ThisWorkbook module and double click that. The macro code will display in the right side view pane.
 
Thanks fro that. I really think I have done it, but it doesn't seem to work.

Can you see where I have gone wrong? I have pasted the code onto the worksheet where I thought it should go
 

Attachments

  • Book1.xlsm
    33 KB · Views: 4
You placed the code in the sheet module,you have to place it in the ThisWorkbook module as stated in post #4 (see screenshot)

84438
 
Back
Top