+ Reply to Thread
Results 1 to 10 of 10

Run macro on close of the document

  1. #1
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Run macro on close of the document

    I have this code below that deletes specific sheets on a workbook. I want this macro to be able to run whenever I close the document and save it with these sheets deleted.

    Formula: copy to clipboard
    Please Login or Register  to view this content.

  2. #2
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,865

    Re: Run macro on close of the document

    Try placing the code in the Before Save Event for the Workbook
    Alan עַם יִשְׂרָאֵל חַי


    Change an Ugly Report with Power Query
    Database Normalization
    Complete Guide to Power Query
    Man's Mind Stretched to New Dimensions Never Returns to Its Original Form

  3. #3
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Run macro on close of the document

    Does this work?
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  4. #4
    Forum Moderator alansidman's Avatar
    Join Date
    02-02-2010
    Location
    Steamboat Springs, CO
    MS-Off Ver
    MS Office 365 Version 2404 Win 11 Home 64 Bit
    Posts
    23,865

    Re: Run macro on close of the document

    What happens when you close the spreadsheet? I think you have to tell us if it works for you. I would remove or comment out the line
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Run macro on close of the document

    I implemented the code and it didn't delete the sheets when I closed the document. Do I have to activate the macro before the close?

  6. #6
    Forum Expert
    Join Date
    06-12-2012
    Location
    Ridgefield Park, New Jersey
    MS-Off Ver
    Excel 2003,2007,2010
    Posts
    10,241

    Re: Run macro on close of the document

    As Alan said you should comment out Sub Deletesheets. Or you could call it instead.

    Please Login or Register  to view this content.

  7. #7
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Run macro on close of the document

    Sorry forgot to mention I did comment out the Sub DeleteSheets

  8. #8
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Run macro on close of the document

    The macro should work if you comment out the second sub statement. For what it's worth I'd use something like this to account for if one of the sheets doesn't exist.
    Please Login or Register  to view this content.
    If what you are using doesn't work I'd suggest checking that you have placed it in the "ThisWorkbook" object and not a worksheet object or module in the vba project. (Ctrl+R to view project explorer and then under the project for the workbook you are using you need "microsoft excel objects" and then "thisworkbook").

  9. #9
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Run macro on close of the document

    Thanks yud. Are you not able to store this type of macro in a personal xls folder or does it have to be specific to your actual worksheet?

  10. #10
    Forum Expert
    Join Date
    04-22-2013
    Location
    .
    MS-Off Ver
    .
    Posts
    4,418

    Re: Run macro on close of the document

    If you put in your personal workbook it would only run when you close the personal workbook.

    By your question I assume you want it to apply to every workbook you open and not have to put it in the code of each workbook?

    Maybe try having a look here: http://www.cpearson.com/excel/AppEvent.aspx

  11. #11
    Registered User
    Join Date
    06-26-2013
    Location
    Northern Ireland
    MS-Off Ver
    Excel 2003, 07, 10
    Posts
    92

    Re: Run macro on close of the document

    It only has to run for specific workbooks with the three sheet names in them. But I understand my mistake now and thanks for that link, very helpful.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] macro to close document but it asks to save changes
    By andnew131 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-26-2012, 01:12 PM
  2. Macro to close down word and re-open a previously saved document
    By snoopy1461 in forum Word Programming / VBA / Macros
    Replies: 1
    Last Post: 01-20-2011, 09:59 AM
  3. Perform a macro on document close?
    By mpower87 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-11-2010, 11:34 PM
  4. [SOLVED] excel macro-close this document
    By chartasap in forum Excel General
    Replies: 2
    Last Post: 04-29-2006, 04:45 PM
  5. [SOLVED] Why does only one document close without asking to be saved?
    By freyabeads in forum Excel General
    Replies: 0
    Last Post: 02-22-2006, 05:10 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1