+ Reply to Thread
Results 1 to 8 of 8

Code to invoke a Macro when the user leaves the active sheet

  1. #1
    Forum Contributor
    Join Date
    04-15-2008
    Location
    Texas, USA
    MS-Off Ver
    M365 Excel Version 2210
    Posts
    198

    Code to invoke a Macro when the user leaves the active sheet

    I have a macro to sort a named range that works well but I need to have it run every time when a user leaves the active sheet. That way if they change anything in the named range, when they leave that sheet, it will re-sort that range. The code below is what I have to sort the range.

    If anyone can help with my question or can simplify my code below, I would be so grateful.
    RR
    Please Login or Register  to view this content.
    Last edited by 6StringJazzer; 06-11-2022 at 08:46 AM. Reason: Good effort but I wanted the post edited, not a duplicated post

  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,750

    Re: Code to invoke a Macro when the user leaves the active sheet

    Your code is not complete; it is just a fragment. What Sub is it in? What module is it in?

    when a user leaves the active sheet
    When the user leaves any active sheet? Or is there one sheet in particular you are interested in?

    If it's one sheet in particular use this code in the VBA module for that sheet:

    Please Login or Register  to view this content.
    Also your code looks like it is from the macro recorder, so has some bloat. I would put it in the code module for sheet Project Role and rewrite it like this:

    Please Login or Register  to view this content.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    04-15-2008
    Location
    Texas, USA
    MS-Off Ver
    M365 Excel Version 2210
    Posts
    198

    Re: Code to invoke a Macro when the user leaves the active sheet

    Sorry Jeff for the confusion. As you can tell, I know VERY LITTLE about macros so I REALLY appreciate your help.

    The range that I am trying to sort is on a sheet called "Project Role". This range named "Proj_Role" is used as a list for input purposes on other sheets and thus the contents need to be in Alphabetical order. If one of the users inserts a row with a new Role or changes a current Role name in the current list, when they leave the "Project Role" sheet, I want the range to be sorted at that point. I don't know how to answer your questions so I'll include the new version which has what I hope to be the properly inserted revision to my previously recorded code.

    Please Login or Register  to view this content.

  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,750

    Re: Code to invoke a Macro when the user leaves the active sheet

    All the code you are showing here should be in the VBA module for the worksheet Project Role. The new sub I provided would be in that module and revised to be:
    Please Login or Register  to view this content.
    Your sort code is hard-coded to sort rows 4-42 so if someone inserts a new row it won't work correctly anymore. If you attach your file here (see yellow banner at the top of the page) I can revise your code to use the name Proj_Role. I can't test code without the file and data.

  5. #5
    Forum Contributor
    Join Date
    04-15-2008
    Location
    Texas, USA
    MS-Off Ver
    M365 Excel Version 2210
    Posts
    198

    Re: Code to invoke a Macro when the user leaves the active sheet

    Thanks again for your help and patience. I attached my file.

    RR
    Attached Files Attached Files

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

    Re: Code to invoke a Macro when the user leaves the active sheet

    See how the data on Sector is a Table? That is what I would do with Project Role, instead of making it a named range. But I didn't change it.

    I did change the code to refer to Proj_Role. I moved the Macro3 into the sheet module and renamed it. Sub Macro3 still exists but now calls the new sub, which will allow you to invoke it from the user interface. I removed Module2, it was redundant. I added the Worksheet_Deactivate Sub.

    I didn't touch Module1, not sure what you are trying to do there.

    On Project Roles I changed (Bottom) to zz(Bottom) so it will always sort to the bottom of the list.

    I tested everything and it works.
    Attached Files Attached Files

  7. #7
    Forum Contributor
    Join Date
    04-15-2008
    Location
    Texas, USA
    MS-Off Ver
    M365 Excel Version 2210
    Posts
    198

    Re: Code to invoke a Macro when the user leaves the active sheet

    Thanks for all your help. It works as I hoped it would; no surprise to you though.

    I'm sure I'll have other questions as this model expands.

    RR

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

    Re: Code to invoke a Macro when the user leaves the active sheet

    Glad to help! If a member helped you solve your problem, consider adding to their reputation by clicking addreputationiconsmall.jpg below their name.

+ 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 only run on sheet1 IF the active sheet, what code?
    By thecdnmole in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 01-05-2019, 05:34 PM
  2. Replies: 1
    Last Post: 04-18-2017, 10:19 PM
  3. [SOLVED] VBA code for protecting Worksheet leaves sheet unprotected if applied twice
    By bdouglas1011 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-03-2014, 07:28 PM
  4. Macro leaves chart active - cannot print to pdf
    By gtiguy in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-26-2012, 04:51 AM
  5. [SOLVED] How to get (range)macro to always run code on a specific sheet regardless of active sheet?
    By JTwrk in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 03-18-2012, 04:33 PM
  6. Replies: 0
    Last Post: 03-01-2012, 12:13 PM
  7. Send the mail from an active sheet - VB code for macro
    By Geetha Ramakrishnan in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 04-11-2011, 07:23 AM

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