+ Reply to Thread
Results 1 to 16 of 16

call macro via formula

  1. #1
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200

    call macro via formula

    first time - forgive errors.

    I want to compare the values of two cells and if not the same, run a macro. I'm trying to not have to click any buttons or use Excel to manually run the macro. Any ideas?
    Last edited by bstubbs; 01-13-2009 at 08:54 AM.

  2. #2
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531
    Assuming the 2 values are on the same sheet you can use a Worksheet Event -- which one will depend upon how the 2 values are being altered/updated ... if they are updated manually then I would advocate the Worksheet_Change event else the Worksheet_Calculate event to invoke your subsequent sub routine... using the former is preferable given it will be invoked fewer times.

    Let us know.

  3. #3
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200
    I am new to Excel and when you say "Worksheet event", I don't know for sure what you're talking about. Do we have a tutorial that will show me something about it? If you could also prototype some code for me, once I figure out the event, it would be a tremendous help. TIA

  4. #4
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531
    If you could elaborate with regard to my earlier Q as to how the 2 values in question are "altered" that would help - as the event you need to use will differ -- if the values are altered by means of formulae you will need to use a Calculation event, if they are altered manually you will use the Change event.

  5. #5
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Have a look at this. It is triggered by changing A1 or B1
    Attached Files Attached Files
    Hope that helps.

    RoyUK
    --------
    For Excel Tips & Solutions, free examples and tutorials why not check out my web site

    Free DataBaseForm example

  6. #6
    Forum Expert
    Join Date
    11-27-2007
    Location
    New Jersey, USA
    MS-Off Ver
    2013
    Posts
    1,669

    macro via formula

    Roy,
    your macro is triggered by changing A1 OR A2.
    didn't want bstubbs to get confused.
    modytrane.

  7. #7
    Forum Expert royUK's Avatar
    Join Date
    11-18-2003
    Location
    Derbyshire,UK
    MS-Off Ver
    Xp; 2007; 2010
    Posts
    26,200
    Well spotted. Thanks

    Code triggered by A1 & B1

    Please Login or Register  to view this content.
    Last edited by royUK; 01-12-2009 at 11:20 AM.

  8. #8
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200
    Ok - I'm trying to work through the concept and understand - a problem for me sometimes. ;->

    I right-click on the page of the workSheet involved and select "code". Enter the code prototyped. Then, assuming no bugs have hatched and I typed correctly, this code would auto-magically execute whenever those two cells are the same. Big picture - is this correct?

    Thanks, again.

    Bob

  9. #9
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    I right-click on the page of the workSheet involved and select "code"
    View Code
    Then, assuming no bugs have hatched and I typed correctly, ...
    Don't type it, copy and paste from the post.
    this code would auto-magically execute whenever those two cells are the same.
    The code will execute whenever either A1 or B1 is changed manually (not as the result of a formula).

    Your code (MyMacro) will be called if the cells are DIFFERENT. If you want it to be called when the are the same, change <> to =
    Entia non sunt multiplicanda sine necessitate

  10. #10
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200
    One last question for you: how would the code be changed to use a single-cell named range from the spreadsheet rather than an explicit address?

    Thanks, again.

    Bob

  11. #11
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Right now it looks specifically at A1 and B1 and tests whether they are equal. I don't understand the logic you would want to apply with a single-cell range.

  12. #12
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200
    If the cell location within the underlying spreadsheet changes, then I would also have to change to hard-coded address of the cells to be evaluated. If, however, the reference was to a named-range, that would presumably work even with the new location of the cell. Or am I missing something on the way that the VBA would work?

    TIA.

    Bob

  13. #13
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    If, however, the reference was to a named-range, that would presumably work ...
    Of course, that's not the question.

    how would the code be changed to use a single-cell named range
    The current code checks if TWO cells are equal each time EITHER of them changes.

    How do you translate that logic to ONE cell?

  14. #14
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200
    I would be comparing the values within two cells, not just a single one. What I'm envisoning is the comparison of two single-celled named-ranges instead of two hard-coded single-cell addresses.

  15. #15
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678
    Got it. Maybe this:
    Please Login or Register  to view this content.
    ... where cell1 and cell2 are your two single-cell named ranges.

  16. #16
    Forum Contributor
    Join Date
    01-09-2009
    Location
    Cedar Hill, Tx
    MS-Off Ver
    Excel 2003
    Posts
    200
    Thanks for all the help and understanding of newbie status.

    Bob

+ 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