+ Reply to Thread
Results 1 to 5 of 5

Create custom command to stop code at any point

  1. #1
    Registered User
    Join Date
    07-03-2013
    Location
    New England
    MS-Off Ver
    Excel 2010
    Posts
    24

    Create custom command to stop code at any point

    Hi All, I am trying to get a piece of macro that will allow me to create a command which ends the macro while running. I know that the standard command is ctrl+break, but I don't want my users to end the macros prematurely so I disabled that command like this:


    Please Login or Register  to view this content.
    Now I would like to have the option to stop the code while I am running it by creating something else which causes the script to stop. My idea would be something like this but I'm not quite sure how to write it properly:


    Please Login or Register  to view this content.
    Last edited by sczerniak; 07-15-2013 at 03:12 PM. Reason: I solved my own problem

  2. #2
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Create custom command to stop code at any point

    Hi,

    There's a much simpler way...

    Please Login or Register  to view this content.
    And assign a button that when triggered changes the UserStopped to True.

    abousetta
    Please consider:

    Thanking those who helped you. Click the star icon in the lower left part of the contributor's post and add Reputation.
    Cleaning up when you're done. Mark your thread [SOLVED] if you received your answer.

  3. #3
    Registered User
    Join Date
    07-03-2013
    Location
    New England
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Create custom command to stop code at any point

    I'm not quite sure I fully understand what you mean. The macro I am using changes between sheets pretty quickly so I'm not sure I would even be able to click a button if I needed to exit the macro.

  4. #4
    Forum Guru
    Join Date
    03-12-2010
    Location
    Canada
    MS-Off Ver
    2010 and 2013
    Posts
    4,418

    Re: Create custom command to stop code at any point

    Use DoEvents... when you have that in place (e.g. during loops) the macro always stops to check if there are any other commands before continuing.

    For example:

    Please Login or Register  to view this content.
    As the code is running, click on Esc. Normally if there is a loop in place, nothing will happen. Because of DoEvents, you will get a msgbox saying that the code was interrupted. Same concept but put an if statement before the Doevents.

  5. #5
    Registered User
    Join Date
    07-03-2013
    Location
    New England
    MS-Off Ver
    Excel 2010
    Posts
    24

    Re: Create custom command to stop code at any point

    I figured out a way to do it within Application.EnableCancelKey. Basically, when the user tries to exit the macro a box comes up asking them to enter the escape code. If they enter the escape code right, then they can exit.

    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)

Similar Threads

  1. Create a command button with code with a command button
    By jakara in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-29-2013, 01:28 PM
  2. Macro to create Command Button and assign code
    By dagindi in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-07-2011, 09:19 AM
  3. Replies: 2
    Last Post: 06-27-2011, 10:47 PM
  4. How to create a command button and assign code to it using VBA?
    By surotkin in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 03-04-2007, 10:05 PM
  5. Using code from custom menu bar to a Command button
    By Alex Martinez in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-27-2005, 08:05 AM

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