Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Excel VBA > Excel VBA
|
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 May 31st, 2013, 02:12 PM
Registered User
 
Join Date: May 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Autmatic run macro, closing workbook.

Dear all.

I would like a macro to run automatically if the workbook closes.

The macro takes care of 2 things:
- Saving the data;
- Send an automated E-mail 'End Of Day' report.

Here is what I have so far:

Code:
Sub Run_On_Close()
Dim Msg, Style, Title, Help, Ctxt, Response, MyString
Msg = "Save & close workbook and e-mail report?" ' Define message.
Style = vbYesNo + vbCritical + vbDefaultButton2 ' Define buttons.
Title = "Run close sequence?" ' 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" Sub SendIndividual().
    ThisWorkbook.Close
       
Else ' User chose No.
    MyString = "No" ' Perform some action.
End If
End Sub

Public Sub SendIndividual()
    'Clear out any old data on Report
    Sheets("Report").Select
    Range("A1").CurrentRegion.ClearContents
    ' Sort data by region
    Sheets("Data").Select
    Range("A1").CurrentRegion.Select
    Selection.Sort Key1:=Range("A2"), Header:=xlYes
    ' Process each record on Distribution
    Sheets("Distribution").Select
    FinalRow = Range("A15000").End(xlUp).Row
    For i = 2 To FinalRow
        Sheets("Distribution").Select
        RegionToGet = Range("A" & i).Value
        Recipient = Range("B" & i).Value
        ' Clear out any old data on Report
        Sheets("Report").Select
        Range("A1").CurrentRegion.ClearContents
        ' Get records from Data
        Sheets("Data").Select
        Range("A1").CurrentRegion.Select
        ' Turn on AutoFilter, if it is not on
        If ActiveSheet.AutoFilterMode = False Then Selection.AutoFilter
        ' Filter the data to just this region
        Selection.AutoFilter Field:=1, Criteria1:=RegionToGet
        ' Select only the visible cells and copy to Report
        Selection.SpecialCells(xlCellTypeVisible).Select
        Selection.Copy Destination:=Sheets("Report").Range("A1")
        ' Turn off the Autofilter
        Selection.AutoFilter
        ' Copy the Report sheet to a new book and e-mail
        Sheets("Report").Copy
        Application.Dialogs(xlDialogSendMail).Show _
            arg1:=Recipient, _
            arg2:="Report for " & RegionToGet
        ThisWorkbook.Close SaveChanges:=True
    Next i
End Sub
 
Old June 1st, 2013, 04:13 AM
Friend of Wrox
 
Join Date: Sep 2005
Posts: 812
Thanks: 1
Thanked 53 Times in 49 Posts
Default

Hi

Can you please try Workbook_BeforeClose event. It should help you

Cheers
Shasur
__________________
C# Code Snippets (http://www.dotnetdud.blogspot.com)

VBA Tips & Tricks (http://www.vbadud.blogspot.com)





Similar Threads
Thread Thread Starter Forum Replies Last Post
opening a workbook in a macro jeskit Excel VBA 1 January 18th, 2011 10:53 AM
Possible to run macro from another workbook? rmilward Excel VBA 2 September 26th, 2009 08:00 PM
Closing Excel Workbook rekha_jsr Excel VBA 0 July 9th, 2005 02:40 AM
personal macro workbook helmekki Excel VBA 0 May 31st, 2004 03:11 PM





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