Results 1 to 8 of 8

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

Threaded View

  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
        Sheets("Project Role").Select
        Application.Goto Reference:="R3C2"
        Range(Selection, Selection.End(xlDown)).Select
        Range("B3:B42").Select
        ActiveWorkbook.Worksheets("Project Role").Sort.SortFields.Clear
        ActiveWorkbook.Worksheets("Project Role").Sort.SortFields.Add Key:=Range("B3" _
            ), SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:= _
            xlSortTextAsNumbers
        With ActiveWorkbook.Worksheets("Project Role").Sort
            .SetRange Range("B4:B42")
            .Header = xlNo
            .MatchCase = False
            .Orientation = xlTopToBottom
            .SortMethod = xlPinYin
            .Apply
        End With
        Range("A1").Select
    Last edited by 6StringJazzer; 06-11-2022 at 08:46 AM. Reason: Good effort but I wanted the post edited, not a duplicated post

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