+ Reply to Thread
Results 1 to 7 of 7

How do I make a macro conditionally run based on the defined name of a cell

  1. #1
    Registered User
    Join Date
    11-05-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    56

    How do I make a macro conditionally run based on the defined name of a cell

    I want to set an if statement on my macro, if a cell meets a certain condition, do not run macro, else run macro

    unimportant background info
    I have a macro that i'm writing where the user selects a work package executes the macro then that macro selects a range of cells and copies them, then pastes the values then selects the cell in column A 6 rows down, the macro then executes again doing the same thing as long as the user holds down the shortcut key. I'm also writing an alternate macro that does the exact same thing except instead of replacing the cells with values it replaces the cells with the formulas. I don't want the macro to be automatic at this time, i want it to have to be ran on each work package, that way the user can easily run it on the whole list of work packages if they hold the shortcut key down or it can easily be executed on just one work package by hitting the shortcut key once.


    the idea is that the user starts at the top, holds down the shortcut key for the macro and the macro keeps executing until it has executed on the last work package. After executing in the last work package the cell it selects 6 rows down will have a defined name of "STOP"

    when it reaches that last cell with the name of STOP rather than executing it should cancel itself/not execute and instead return a message that says "End of worksheet reached, Macro can not be executed from this cell". It doesn't need to be a special form control text box, just if there is an easy way to make it show that message in the standard grey excel macro debug text box that usually pops up on errors would be great so that the user knows what's going on or that they reached the end.

    /unimportant background info

    this is my code:

    Please Login or Register  to view this content.
    this is what i want it to do
    So I want it to be something along the lines of:

    If Active.Selection (or currently selected cell/don't know the syntax) = "STOP" (defined name of the cell is stop, not the cell value/contents)

    Do not run macro and return message saying you have reached end of spreadsheet and macro cannot be executed from this cell.

    ElseIf Active.Selection <> "STOP" Then

    Execute the macro/above lines of code as it's written.


    So if defined name = stop, don't run macro, else if, run macro

    and if i wanted to how would i make such code check multiple condtions. Like an if statement with (and) or (or) in it


    Attached is a sample spreedsheet i'm working with. I only have two work packages added at the moment but a user could have only a few or a couple hundred in the spreadsheet which is why i want the user to be able to hold down the macro shortcut key and have it keep executing till it reaches the end and then stop without the user having to pay particular attention or worry about breaking the spreadsheet by running the macro after it has reached the end.


    Thanks

    and is there a good online resource for how to set conditions or if statements for macros. I'm quite good with if statements but i have no idea how to put if statements into macro form. So i can record most of my macros but don't know how to take it to the next step and make them conditional or error trap them.
    Attached Files Attached Files
    Last edited by JTwrk; 02-17-2012 at 05:12 PM.

  2. #2
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: How do I make a macro conditionally run based on the defined name of a cell

    Not quite sure but this should be close:
    Please Login or Register  to view this content.
    Ben Van Johnson

  3. #3
    Forum Expert
    Join Date
    11-29-2010
    Location
    Ukraine
    MS-Off Ver
    Excel 2019
    Posts
    4,168

    Re: How do I make a macro conditionally run based on the defined name of a cell

    @protonLeah, as the called sub changes selection possibly the line calling another sub should be enclosed with Application.EnableEvents turn on/off lines

  4. #4
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: How do I make a macro conditionally run based on the defined name of a cell

    I agree:
    Please Login or Register  to view this content.

  5. #5
    Registered User
    Join Date
    11-05-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: How do I make a macro conditionally run based on the defined name of a cell

    Thank you, i appreciate the help, wasn't sure what you were doing but it looks like you wrote a macro that calls on my other macro to run if the condition is met.

    i'm trying to embed the condition in the existing macro rather than creating a macro that runs and calls on another macro to run if possible

    So far i have this macro sort of working.

    Please Login or Register  to view this content.
    However I actually have to have the word "STOP" in the cell for it to work. So it's checking on cell value and if it matches it stops and pops up the message box.

    How do i get this part

    Please Login or Register  to view this content.
    to check on defined name/named range instead of value.


    i tried

    If Selection.Range = ("STOP") Then

    &

    If Selection.Name = ("STOP") Then


    but it always gives me an error on that string when i try it

    See attachment, open and enable macros and hit ctrl + Shift + z
    Attached Files Attached Files
    Last edited by JTwrk; 02-17-2012 at 10:40 PM.

  6. #6
    Registered User
    Join Date
    11-05-2011
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    56

    Re: How do I make a macro conditionally run based on the defined name of a cell

    This works too

    Please Login or Register  to view this content.
    however this still doesn't check on defined name.


    Please Login or Register  to view this content.
    this checks on defined name but the macro only works and pops up the message box if i run it from the defined name cell.

    if i try running it from one of the work package cells it pops up an error at me asking to debug.

  7. #7
    Forum Guru
    Join Date
    03-02-2006
    Location
    Los Angeles, Ca
    MS-Off Ver
    WinXP/MSO2007;Win10/MSO2016
    Posts
    12,612

    Re: How do I make a macro conditionally run based on the defined name of a cell

    Please Login or Register  to view this content.

+ 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