+ Reply to Thread
Results 1 to 14 of 14

Is there a better way to call macro on cell value change?

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

    Is there a better way to call macro on cell value change?

    Hi,

    I currently have a macro that checks one cell ("ChangeTrigger") for a change in formula value on a non-active sheet and if there is a change it then fires another macro ("Macro99"). Problem is that while this macro works, it only seems to work for a change in ONE celI and I need it...or better code...to be able to check for a value change in 3 cells and run one of three different macros depending on which cell had a change in value.

    My current code is on the worksheet object:
    Please Login or Register  to view this content.
    And this little pice of code is atop a module:
    Please Login or Register  to view this content.
    Thanks,

    Lawrence

  2. #2
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Is there a better way to call macro on cell value change?

    Try this

    Please Login or Register  to view this content.
    This is worksheet event code, which means that it needs to be
    placed in the appropriate worksheet code module, not a standard
    code module. To do this, right-click on the sheet tab, select
    the View Code option from the menu, and paste the code in.

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

    Re: Is there a better way to call macro on cell value change?

    Bob,

    That code looks very promising. Thank you!

    Two questions: Will the Change event work for a formula result on a non-active worksheet? And how do I get a change in H3 and H5 to fire different macros? for example, if H3 changes, fire Macro99B; and if H5 changes, fire macro99C.

    Thanks again,

    Lawrence

  4. #4
    Forum Expert Bob Phillips's Avatar
    Join Date
    09-03-2005
    Location
    Wessex
    MS-Off Ver
    Office 2003, 2010, 2013, 2016, 365
    Posts
    3,284

    Re: Is there a better way to call macro on cell value change?

    Quote Originally Posted by skysurfer View Post
    Will the Change event work for a formula result on a non-active worksheet?
    I am not sure what you mean by this, can you elaborate?

    Quote Originally Posted by skysurfer View Post
    And how do I get a change in H3 and H5 to fire different macros? for example, if H3 changes, fire Macro99B; and if H5 changes, fire macro99C.
    You have to test each cell individually for an intersection and call the appropriate macro.

  5. #5
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Is there a better way to call macro on cell value change?

    skysurfer,

    a Worksheet_Change event macro only monitors its own worksheet, not other sheets. You need Change events in each sheet to trigger activities in each sheet.

    A Worksheet_Change event also only works on certain events. If a cell value changes as a result of a formula, this does not trigger the Worksheet_Change.

    But the Worksheet_Calculate event would be triggered by a formula that recalculates.

    So, if A1 has a formula, you either monitor the precedents of cell A1 with a Change event, or monitor A1 with a Calculate event.

    cheers

  6. #6
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Is there a better way to call macro on cell value change?

    Quote Originally Posted by teylyn View Post
    a Worksheet_Change event macro only monitors its own worksheet, not other sheets.
    You need Change events in each sheet to trigger activities in each sheet.
    ... or use the Workbook level SheetChange Event (using the Sh variable to determine appropriate action(s))

  7. #7
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Is there a better way to call macro on cell value change?

    DO, good point. I have not used that event much. Would it catch formula recalcs, for example if the precedents are in another workbook?

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Is there a better way to call macro on cell value change?

    The Workbook Change event would only capture changes undertaken on the active sheet (or active sheets if grouped).

    The Workbook Calculate event would trigger for each sheet requiring calculation as and when calculation was invoked (by whatever means)

    I confess I'm not quite sure what you're getting at regards external precedents ?

  9. #9
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Is there a better way to call macro on cell value change?

    not quite sure what you're getting at regards external precedents ?
    Just trying to come up with a scenario where a change in a cell is not triggered by a manual entry, but by a reference to an external workbook, for example.

    Say A1 references to Z1 in another workbook. B1 in the current book performs a calculation, referencing A1. If Z1 in the external file is changed, A1 in the current file will update and B1 will recalculate.

    This will then trigger the Sheet Calculate event. Does it also trigger the Workbook Calculate event? -- I'll go and set up a test scenario tomorrow, then I'll know.

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

    Re: Is there a better way to call macro on cell value change?

    The Workbook doesn't have a Calculate event, so no.
    It will, however, trigger the Workbook_SheetCalculate event...
    Everyone who confuses correlation and causation ends up dead.

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

    Re: Is there a better way to call macro on cell value change?

    Quote Originally Posted by teylyn View Post
    skysurfer,

    a Worksheet_Change event macro only monitors its own worksheet, not other sheets. You need Change events in each sheet to trigger activities in each sheet.

    A Worksheet_Change event also only works on certain events. If a cell value changes as a result of a formula, this does not trigger the Worksheet_Change.

    But the Worksheet_Calculate event would be triggered by a formula that recalculates.

    So, if A1 has a formula, you either monitor the precedents of cell A1 with a Change event, or monitor A1 with a Calculate event.

    cheers

    teylyn,

    So, modifying my original Calculate event code posted in the OP works? How do I get that code to monitor three cells instead of just one?

    [sorry, I'm not that gifted with vba]

    Lawrence

  12. #12
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Is there a better way to call macro on cell value change?

    Lawrence,

    Take another look at Bob Phillips' code. It's all in there!

    cheers

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

    Re: Is there a better way to call macro on cell value change?

    Quote Originally Posted by teylyn View Post
    Lawrence,

    Take another look at Bob Phillips' code. It's all in there!

    cheers

    Cool! Will his Intersect below line work with Worksheet_Calculate? If so, then I'm golden!!!

    Please Login or Register  to view this content.
    Thanks again,

    Lawrence

  14. #14
    Forum Expert teylyn's Avatar
    Join Date
    10-28-2008
    Location
    New Zealand
    MS-Off Ver
    Excel 365 Insider Fast
    Posts
    11,375

    Re: Is there a better way to call macro on cell value change?

    With the caclulate event you don't need to check for an intersect with Target. You can inspect the cells you want to inspect by referencing them directly.

+ 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