Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
Password Reminder
Register
Register | FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old September 5th, 2005, 09: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
Reply With Quote
  #2 (permalink)  
Old September 8th, 2005, 03: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

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

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 08:21 PM
Excel Formula End Range Changes when Macro Is Run maaron Excel VBA 0 October 11th, 2005 11:56 AM
Excel Formula End Range Changes when Macro Is Run maaron Beginning VB 6 0 October 11th, 2005 11:33 AM
close a program using macro greatmanu Excel VBA 1 September 11th, 2003 02:41 PM



All times are GMT -4. The time now is 11:57 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.