+ Reply to Thread
Results 1 to 7 of 7

Trigger macro on leaving sheet

  1. #1
    Registered User
    Join Date
    11-12-2010
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    20

    Exclamation Trigger macro on leaving sheet

    Hi

    I need pivot tables to be refreshed on leaving a specific sheet in my workbook. How do I trigger a macro on leaving a sheet ?

    Currently, the refresh part is being done using a simple macro (refresh all) and the trigger is on a form button.

    Thanks!
    N

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Trigger macro on leaving sheet

    There is no event for leaving a worksheet, but the Worksheet.Activate event occurs if you enter a worksheet. I would recommend writing a sub in a Module that refreshes the pivot tables, then calling that from every worksheet like so:

    Please Login or Register  to view this content.
    May be impractical if you have many worksheets or constantly create new ones.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Trigger macro on leaving sheet

    I thought about this a little more. What I suggested will do the refresh anytime you change worksheets, regardless of where you started from. If you really only want to refresh when you leave a particular worksheet, one solution is to keep a global variable in the Module that keeps track of what sheet you are in at any given time, then you can detect if you left a particular sheet when entering the next one.

    That's pretty sketchy. If that's not clear I can provide some specific code to illustrate.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Trigger macro on leaving sheet

    Well, here is the code. This code generates a message to the user upon leaving Sheet1. Note that there is code in every sheet to support this as described above.
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    11-12-2010
    Location
    Montreal
    MS-Off Ver
    Excel 2007
    Posts
    20

    Re: Trigger macro on leaving sheet

    Hi 6StringJazzer

    Twice you've come up and help in one week - much appreciated !

    Cheers,
    N

  6. #6
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Trigger macro on leaving sheet

    There is no event for leaving a worksheet, ...
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  7. #7
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Trigger macro on leaving sheet

    Oh, man. That's a demerit for me. Sorry.

    My code works nonetheless. But here's the more elegant version.
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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