+ Reply to Thread
Results 1 to 17 of 17

Run a Macro if condition is met

  1. #1
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Run a Macro if condition is met

    Hi, is it possible to run a certain macro when a condition is met? How?
    I have excel 2010.

    What my macro does: Removes the border on a Cell

    What I want it to do : If Cell X has no data in it then it removes the thick box border on Cell Y

    Example:
    If G12 is blank then run Macro1

    *Marco1 Removes border on k20*


    Please help!
    Last edited by NameUse; 11-10-2012 at 01:37 PM.

  2. #2
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: Run a Macro if condition is met

    Is G12 the only cell on which the macro should be run?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  3. #3
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Run a Macro if condition is met

    There are many self-triggering macros you could put into the sheet module, the sheet itself, where this monitoring should take place. You need to indicate what trigger would be best.

    Worksheet_Change - this macro is triggered by you making a manual change to any cell on the sheet. Manual changes trigger this, not calculated changes.

    Worksheet_Calculate - this macro is triggered anytime any cell on the sheet recalculates its value, so only cells with formulas would cause this to trigger.

    Worksheet_Activate - this macro triggers immediately when you navigate to this sheet from another sheet in the workbook.

    Worksheet_Deactivate - this macro triggers as you navigate away from this sheet to another sheet in the workbook.

    Worksheet_BeforeDoubleClick - this macro is triggered by you double-clicking on any cell in the sheet.


    Which of these seems like the best time/way to trigger your test of cell G12?
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  4. #4
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Re: Run a Macro if condition is met

    @arlu1201 The macro should activate when g12 is left blank (And it should remove the border on k20)
    @JBeaucaire G12 will be left blank at the start then once you add data into it the macro turns off

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Run a Macro if condition is met

    That sounds more like Conditional Formatting on cell K20.

    1) Highlight K20
    2) Open the Conditional Formatting window and create a formula-based rule =ISBLANK(G12)
    The formatting would be Borders around the cell

    http://screencast.com/t/JQIthY5C

  6. #6
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Re: Run a Macro if condition is met

    The border must be thick and there are no options for a thick border on conditional formatting, thats why i created a macro to do it.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Run a Macro if condition is met

    Let's see the macro you created.

  8. #8
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Re: Run a Macro if condition is met

    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Re: Run a Macro if condition is met

    Instead of trying to remove the thick border already there, should i make a macro that puts a thick border there?

  10. #10
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Re: Run a Macro if condition is met

    So i have 2 options:
    1. Have a Border but if g12 is blank is gets remove
    2.No border then add it as the user types something into g12
    Last edited by NameUse; 11-10-2012 at 05:01 PM.

  11. #11
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Run a Macro if condition is met

    Try

    Please Login or Register  to view this content.
    HTH
    Regards, Jeff

  12. #12
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Re: Run a Macro if condition is met

    Ok where should i put the code?

  13. #13
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Run a Macro if condition is met

    This is sheet code...

    Right click on Sheet tab >> View Code >> Paste on right side of screen >> Alt + Q

    Now every time you change something in G12, K20 is changed.

    You can use this version to change the thickness of the border

    Please Login or Register  to view this content.
    Where do I paste the code that I want to use in my workbook

  14. #14
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Re: Run a Macro if condition is met

    Sheet tab? You mean developer dont you?

  15. #15
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,318

    Re: Run a Macro if condition is met

    No I mean sheet tab...

    If you right click on the sheet tab you want the code on you will see a menu open which has View Code...

    Did you look at the link I provided?

  16. #16
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Re: Run a Macro if condition is met

    ok, il try that.
    Last edited by NameUse; 11-11-2012 at 12:23 PM.

  17. #17
    Registered User
    Join Date
    11-10-2012
    Location
    London
    MS-Off Ver
    Microsoft Office 2010
    Posts
    18

    Re: Run a Macro if condition is met

    Thank you! It worked! (although i had to edit the code abit, you gave me a great template!)

    A Star for everyone who tried to help!

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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