+ Reply to Thread
Results 1 to 11 of 11

Trigger a macro or worksheet event with a UDF?

  1. #1
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Trigger a macro or worksheet event with a UDF?

    Attached is an interactive chart I’m working on – to change the chart on the “Interactive Dashboard” sheet, all you have to do is mouse over any small triangle on row 6 between Col B – G. I have 9 stocks assigned to five different categories – the category count is displayed on row four.

    My problem is that while Col E actually only should have two diamonds on the chart, the chart is plotting the first 5 lines on the horizontal access. I can solve this problem by filtering Col H on the “Graph” worksheet for blanks. I have a worksheet_change macro to do just that – if you put your cursor in cell H2 of the "Graph" worksheet and hit enter, you can see that macro works.

    My question is how can I trigger those two lines of code from the UDF? I’ve tried adding those lines to the UDF, Calling them from the UDF and even using the worksheet_change event. None of those work – even though the UDF changes cell H2, the worksheet_change event does not fire.

    Does anyone have any ideas? Appreciate any suggestions. Thanks!
    aquixano
    Attached Files Attached Files

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Trigger a macro or worksheet event with a UDF?

    Hi there,

    I haven't yet looked at your workbook, but as far as I know, the ONLY thing that a UDF can do is to change the value of the cell in which it is entered - it can affect nothing other than that

    Regards,

    Greg M

  3. #3
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Trigger a macro or worksheet event with a UDF?

    Thanks Greg. The UDF does change the value of a cell in the workbook - do you have any idea why that doesn't trigger the Worksheet_Change Event?

  4. #4
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,641

    Re: Trigger a macro or worksheet event with a UDF?

    Hi again,

    The Worksheet_Change event is triggered when a new value is ENTERED in a cell, but not when the cell value changes as a result of worksheet recalculation.

    Regards,

    Greg M

  5. #5
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,405

    Re: Trigger a macro or worksheet event with a UDF?

    Perhaps what you want is a calculate event procedure rather than a change event procedure. It does seem that most event code examples on the internet are change event procedures, but it certainly is not the only available event.
    Help file for the calculate event: https://msdn.microsoft.com/en-us/lib.../ff838823.aspx
    Quote Originally Posted by shg
    Mathematics is the native language of the natural world. Just trying to become literate.

  6. #6
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Trigger a macro or worksheet event with a UDF?

    Thanks for the suggestions. I’m running Excel 2010 and from what I’ve been able to find, the calculate event isn’t available before 2013.

    Greg – Are you making a distinction with “Entered” that it has to be keyboard input (vs. changing the cell value thorough a macro)? The UDF is changing the cell value (not just recalculating it) – the same action I’ve used in Excel macros that trigger worksheet change events. Perhaps UDFs disable Events (alternatively I wonder if I need to specify an attribute to allow Events when creating the UDF). I’ll keep searching – appreciate any other thoughts.

  7. #7
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: Trigger a macro or worksheet event with a UDF?

    see attached a different approach

    instead of getting "" as results..enlisted NA() as NA() does not show up on graphs

    slight fiddling around with formulas involved

    J4 used CSE formula to achieve max number
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Conditional formatting to make NA appear white so its still "clean" looking
    Attached Files Attached Files
    Last edited by humdingaling; 03-20-2016 at 09:56 PM.
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  8. #8
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,405

    Re: Trigger a macro or worksheet event with a UDF?

    Thanks for the suggestions. I’m running Excel 2010 and from what I’ve been able to find, the calculate event isn’t available before 2013.
    I would be curious where you are finding this information. I use event code only rarely, but I first started using calculate event code in Excel 2002, so I know it came long before 2013.

  9. #9
    Forum Contributor
    Join Date
    09-21-2014
    Location
    Midwest USA
    MS-Off Ver
    2010
    Posts
    349

    Re: Trigger a macro or worksheet event with a UDF?

    @humdingaling – very nice! On first glance it looks like your approach works – I need to spend a little more time testing to make sure the change didn’t affect anything unintentionally, but thank you for your effort. Also a new trick for me – I don’t use charts often and didn’t know about the NA() trick.

    @MrShorty – the link you provided re: Worksheet.Calculate says that it’s for 2013 and later and under “other versions” nothing is listed. I checked a couple other sites and didn’t find anything that worked with 2010 – you’re right of course, subsequently to my post I did find some sample code for the worksheet.calculate that worked in 2010. My exposure to the worksheet events is somewhat new so I really don’t know how much there is beyond selectionChange/Change/follow hyperlink & now calculate.

    Thanks to both of you for your assistance.

  10. #10
    Forum Guru
    Join Date
    04-13-2005
    Location
    North America
    MS-Off Ver
    2002/XP, 2007, 2024
    Posts
    16,405

    Re: Trigger a macro or worksheet event with a UDF?

    @MrShorty – the link you provided re: Worksheet.Calculate says that it’s for 2013 and later and under “other versions” nothing is listed.
    I see. From what I have seen, Microsoft has never been good at fully cross-referencing their help-files.

    When you are ready to go beyond the events you are familiar with, you might look at this page https://msdn.microsoft.com/en-us/lib.../ff841127.aspx which lists the available events, methods, and properties associated with the Worksheet object.

  11. #11
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Trigger a macro or worksheet event with a UDF?

    In Worksheet code module if you will select Worksheet from left dropdown at top of module then right dropdown will list all available events.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

+ 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] Worksheet Change Event Wrongly Trigger
    By Faridwahidi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-03-2014, 11:00 PM
  2. Replies: 1
    Last Post: 05-27-2011, 12:27 PM
  3. Macro Trigger/Event
    By 31RHCP in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 07-29-2010, 06:18 PM
  4. [SOLVED] Cell value change to trigger macro (worksheet change event?)
    By Neil Goldwasser in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2006, 10:00 AM
  5. [SOLVED] Trigger macro with keypress event??
    By CarlosAntenna in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 09-01-2005, 04:05 PM
  6. How to trigger a macro on a worksheet on the event of user-input?
    By fiven in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2005, 11:38 PM
  7. How to auto-trigger a macro on a worksheet on the event of user-input?
    By fiven in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-19-2005, 11:23 AM

Tags for this Thread

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