+ Reply to Thread
Results 1 to 6 of 6

How to trigger a macro from range not on active sheet

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    How to trigger a macro from range not on active sheet

    Hi,

    Is it possible to trigger a macro from a change in a range of formula cells if that range is NOT on the active sheet?

    Basically, I have a dynamic range of dates on a calculation sheet where the dates can change or dates can be added/deleted, and every time there is any change in that range I need to fire a macro. I found some code that will trigger based on a formula result, but the range had to be on the active sheet. Is there code that can do this?

    Thanks,

    Lawrence

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: How to trigger a macro from range not on active sheet

    Hi just use the sheet change event on the sheet in question and use the following

    IF Not Intersect (Target, your_range) Is Nothing Then
    'your macro
    
    End If
    HTH
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  3. #3
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    Re: How to trigger a macro from range not on active sheet

    That works beautifully!

    And it's so simple. Many of the macros I found included additional code. Do you know what the bold lines below mean?

    Private Sub Worksheet_Change(ByVal Target As Range)
        
        On Error GoTo ErrH
    
        'Check target to determine if macro is triggered
        If Not Intersect(Target, Range("Database01_Dates")) Is Nothing Then
    
        Application.EnableEvents = False
            'Do things as a result of a change
            Database01_EOMONTHFill
            Database01_ClearAfterLastRow
    
        End If
    
    ErrH:
        Application.EnableEvents = True
    
    End Sub
    Thanks for your help!

    Lawrence

  4. #4
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    Re: How to trigger a macro from range not on active sheet

    Quote Originally Posted by Richard Buttrey View Post
    Hi just use the sheet change event on the sheet in question and use the following

    IF Not Intersect (Target, your_range) Is Nothing Then
    'your macro
    
    End If
    HTH

    Ooops...Hold it. I can't get the macro to fire when the sheet where the range is located isn't the active sheet and that formula results change. Is my code above missing something?

    Lawrence

  5. #5
    Forum Contributor
    Join Date
    05-24-2006
    Location
    Los Angeles
    MS-Off Ver
    2019
    Posts
    328

    Re: How to trigger a macro from range not on active sheet

    Okay, I've narrowed this down. The above code only works (the macro only fires) when a cell in the range is manually keyed. What I need is to also have the macro fire when the formal result changes.

    Is that a calculation event os some kind???

    Lawrence

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    21,968

    Re: How to trigger a macro from range not on active sheet

    You need the Worksheet_Calculate event. Note that this does not provide you with a range argument, nor does it mean necessarily that any of the values have actually changed. If possible, I would monitor the inputs to the formulas rather than the formula cells themselves, since you can use the Change event for that.
    Everyone who confuses correlation and causation ends up dead.

+ 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