Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
| Search | Today's Posts | Mark Forums Read
Excel VBA Discuss using VBA for Excel programming.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Excel VBA section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 5th, 2005, 08:37 AM
Registered User
 
Join Date: Sep 2005
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Run macro when exit/close excel

hi,

i want to run a macro whenever i decide to close excel sheet. is there a way how to do it?

this is my coding so far, and as i said, i want to run this macro whenever i close the worksheet.

Sub test()
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Please make sure you have updated the DATE!" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "DATE" ' Define title.
Help = "DEMO.HLP" ' Define Help file.
Ctxt = 1000 ' Define topic
        ' context.
        ' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
    MyString = "Yes" ' Perform some action.
    ThisWorkbook.Close
    
Else ' User chose No.
    MyString = "No" ' Perform some action.
End If
End Sub
 
Old September 8th, 2005, 02:42 AM
Friend of Wrox
 
Join Date: Jan 2005
Location: Bournemouth, Dorset, United Kingdom.
Posts: 180
Thanks: 0
Thanked 0 Times in 0 Posts
Default

1 open the VB Editor.

2 View the Project Explorer if not already visible.

3 Right click over the "ThisWorkbook" object. Select 'View code'.

4 In the Code page, add the following code...
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Please make sure you have updated the DATE!" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "DATE" ' Define title.
Help = "DEMO.HLP" ' Define Help file.
Ctxt = 1000 ' Define topic
        ' context.
        ' Display message.
Response = MsgBox(Msg, Style, Title, Help, Ctxt)
If Response = vbYes Then ' User chose Yes.
    MyString = "Yes" ' Perform some action.
    ThisWorkbook.Close

Else ' User chose No.
    MyString = "No" ' Perform some action.
End If
End Sub


Cheers

Matt





Similar Threads
Thread Thread Starter Forum Replies Last Post
Check Box Run Macro on Exit David Mulvey Word VBA 2 January 8th, 2007 05:53 PM
Exit or Close Internet Explorer rylemer Classic ASP Basics 1 July 12th, 2006 07:21 PM
Excel Formula End Range Changes when Macro Is Run maaron Excel VBA 0 October 11th, 2005 10:56 AM
Excel Formula End Range Changes when Macro Is Run maaron Beginning VB 6 0 October 11th, 2005 10:33 AM
close a program using macro greatmanu Excel VBA 1 September 11th, 2003 01:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.