+ Reply to Thread
Results 1 to 4 of 4

Run macro if TRUE

  1. #1
    Registered User
    Join Date
    12-03-2012
    Location
    Leeds, UK
    MS-Off Ver
    Excel 2003
    Posts
    15

    Run macro if TRUE

    Hi,

    Can anyone point me in the right direction with this please?

    I want to run a macro if the result of an IF function is true.

    E.g. cell J55 contains =IF(H55>I55,"Goodbye","")

    H55 contains =NOW()

    I55 contains =DATE(2012,12,31)

    Now by changing the dates etc. it prints Goodbye.... simple enough, but what I want is for it to run a macro that selects a range from the previous year's figures and simply changes the fill colour on the range. The macro for this also works fine. I just can't see how to make the macro run, rather than printing goodbye.

    Can it be done please?

    Cheers,

  2. #2
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Run macro if TRUE

    You would need an Event macro. The Event macro monitors cell J55 and when J55 becomes "Goodbye", the macro will run your macro. Lets say your macro is called ColorRange.

    Enter the following in the worksheet code area:

    Please Login or Register  to view this content.
    Because it is worksheet code, it is very easy to install and automatic to use:
    1. right-click the tab name near the bottom of the Excel window
    2. select View Code - this brings up a VBE window
    3. paste the stuff in and close the VBE window
    If you have any concerns, first try it on a trial worksheet.

    If you save the workbook, the macro will be saved with it.


    To remove the macro:
    1. bring up the VBE windows as above
    2. clear the code out
    3. close the VBE window
    To learn more about macros in general, see:

    http://www.mvps.org/dmcritchie/excel/getstarted.htm

    To learn more about Event Macros (worksheet code), see:

    http://www.mvps.org/dmcritchie/excel/event.htm

    Macros must be enabled for this to work!
    Last edited by Jakobshavn; 12-05-2012 at 08:13 AM.
    Gary's Student

  3. #3
    Registered User
    Join Date
    12-03-2012
    Location
    Leeds, UK
    MS-Off Ver
    Excel 2003
    Posts
    15

    Re: Run macro if TRUE

    Thanks for the help, but I'm still a bit confused...

    If I view the code I can see my macro, but do I paste your code into the same window above my macro code, or should it go in a seperate window?

    Sorry if this is basic stuff, but I've never seen or used vba before.

    Cheers,

  4. #4
    Forum Expert Jakobshavn's Avatar
    Join Date
    08-17-2012
    Location
    Lakehurst, NJ, USA
    MS-Off Ver
    Excel 2007
    Posts
    1,970

    Re: Run macro if TRUE

    They go in different code locations.
    See my comments about right-clicking the tab name.
    See the attached workbook for an example.
    Attached Files Attached Files

+ 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