+ Reply to Thread
Results 1 to 7 of 7

Trigger a macro on external link update

Hybrid View

  1. #1
    Registered User
    Join Date
    01-05-2015
    Location
    Kosice, Slovakia
    MS-Off Ver
    2010
    Posts
    7

    Trigger a macro on external link update

    I need an advice on how to trigger a macro when pressing the update button in external links window (in excel).

    As far as I know, there is no such event which would do the job.

    I know that there is a possibility to create a custom event, however I have no experience with programming such complex classes, and in addition I'm not even sure if this is possible.

    Any suggestions would be highly appreciated.

  2. #2
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,289

    Re: Trigger a macro on external link update

    One way is to link one (or more) cell(s) to your data table, and use an equal number of cells to store values as a check. Then use the worksheet's calculate event to compare the linked value to the stored value. This example is for a link in cell A1, using A2 to store the comparison value, and your macro is named MacroToTrigger. Of course, if the external link has not changed, then the macro will not be triggered. In that case, you would need to use a macro that updates the external link and runs your macro.


    1) Copy this code.
    2) Right-Click the sheet tab of interest.
    3) Select "View Code"
    4) Paste the code into the window that appears.
    5) Save the file as a macro-enabled .xlsm file.
    6) Make changes as needed


    Private Sub Worksheet_Calculate()
    
        If Range("A1").Value = Range("A2").Value Then Exit Sub
        
        Application.EnableEvents = False
        MacroToTrigger
        Range("A2").Value = Range("A1").Value
        Application.EnableEvents = True
    End Sub
    Bernie Deitrick
    Excel MVP 2000-2010

  3. #3
    Registered User
    Join Date
    01-05-2015
    Location
    Kosice, Slovakia
    MS-Off Ver
    2010
    Posts
    7

    Re: Trigger a macro on external link update

    Hi Bernie,

    thanks for the time taken to reply.
    Unfortunately this doesn't solve my problem. I need a solution on a workbook level (was testing also the SheetCalculate and SheetChange in the ThisWorkbook event section, but if the refreshed value doesn't change, the event is not triggered).

    To be more precise, i need to get somehow the timestamp of the refreshed link. This would be easy, if the link would be refreshed via code, but in my case this is not acceptable.
    I'm building an add-in, in which this information would be stored, and then later when the specific functionality (macro in the background) is called by the user, this timestamp would be used as an input parameter.

    (hope the explanation was clear )

  4. #4
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,289

    Re: Trigger a macro on external link update

    IF the value hasn't changed, has the link really been refreshed ?

    "This would be easy, if the link would be refreshed via code, but in my case this is not acceptable."

    Sometimes you need to change how you allow people to use Excel - you want to use my app, you play by my rules. OR you change what you are linking to, to capture a value that will have changed, like a timestamp in the source data.

  5. #5
    Registered User
    Join Date
    01-05-2015
    Location
    Kosice, Slovakia
    MS-Off Ver
    2010
    Posts
    7

    Re: Trigger a macro on external link update

    Yep that would be possible, but these are workarounds.
    I still hope I will figure out a nice and neat solution...if there is an event which can be raised by for ex. a sheet change, why wouldn't it be possible to create a custom event which would be triggered by the built-in button.

    In general I think that almost everything is possible, well...until its not

  6. #6
    Forum Expert
    Join Date
    02-11-2014
    Location
    New York
    MS-Off Ver
    Excel 365 (Windows)
    Posts
    6,289

    Re: Trigger a macro on external link update

    If you convert to a power query instead of a link, then you could use the AfterRefresh event of the QueryTable object.

  7. #7
    Registered User
    Join Date
    01-05-2015
    Location
    Kosice, Slovakia
    MS-Off Ver
    2010
    Posts
    7

    Re: Trigger a macro on external link update

    So I found a code on the internet & changed it a bit and I think I'm almost there...

    The code uses a custom class which raises an event for a specified command.
    In the example below i use an ID 21 which refers to the "Cut" command - i have chosen it for testing because the id 21 is the same both for the context menu and the tab menu as well.

    It works perfectly when you right click on a cell and press cut (in the context menu), but for some reason I cannot make it work for the Home Tab button.

    Any thoughts on what is the issue here?


    --> Paste to ThisWorkbook event window:

    Private Sub Workbook_Open()
        InitEvents
    End Sub
    --> Create a Class named Class1 and paste the below:

    Public WithEvents cmdCalculate As Office.CommandBarButton
    
    Private Sub cmdCalculate_Click(ByVal cmdCalculate As CommandBarButton, CancelDefault As Boolean)
        MsgBox "Hello"
    End Sub
    --> Create a normal module and paste the below:

    Private clsCBClass As New Class1
    
    Sub InitEvents()
    Dim cmdCalculate As CommandBarButton
    Set clsCBClass.cmdCalculate = Application.CommandBars.FindControl(ID:=21)
    End Sub

+ 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. Automatic External Link Update
    By erikw48 in forum Excel General
    Replies: 0
    Last Post: 02-11-2015, 02:43 PM
  2. Automatic External Link Update
    By erikw48 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-11-2015, 11:18 AM
  3. Run Macro with external laser trigger
    By qikslvr in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-12-2014, 04:22 PM
  4. Update external data link (webdata)
    By billydilly in forum Excel General
    Replies: 1
    Last Post: 11-22-2012, 10:55 AM
  5. If Macro finds no external link supress Update Value Prompt
    By dieseldogpi in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 09-27-2010, 09:13 AM
  6. List and Update of Excel External Link
    By skorde in forum Excel General
    Replies: 1
    Last Post: 05-04-2009, 02:19 AM
  7. Mass update external link
    By lhh_heather in forum Excel General
    Replies: 1
    Last Post: 01-18-2007, 06:54 PM

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