+ Reply to Thread
Results 1 to 3 of 3

Trigger to exit macro based on user input of Designated Key.

  1. #1
    Registered User
    Join Date
    07-22-2017
    Location
    Wisconsin, US
    MS-Off Ver
    2016
    Posts
    4

    Trigger to exit macro based on user input of Designated Key.

    I'd like to see any example of exiting a macro based on the user typing the letter "K" for example, while the macro is still running.

    I understand the escape key works but it also brings up a message box. Is there any code sequence, that would allow a macro to exit based on a user typing the letter "K" at any point INSIDE of the macro's progress? And do without any further error messages?
    Or, even to use the escape key but have the message box immediately removed without further user input (not having to select "ok")?

    I do apologize for an earlier post related to my same project that was not very well-developed and presented. But in short, if I can't solve this question, I think I can cntr+alt+delete my entire project idea .

  2. #2
    Forum Expert
    Join Date
    01-23-2013
    Location
    USA
    MS-Off Ver
    Microsoft 365 aka Office 365
    Posts
    3,863

    Re: Trigger to exit macro based on user input of Designated Key.

    Hi thaupert and welcome to ExcelForum,

    You can trap any keystroke (such as 'K'), but it requires advanced techniques that may become unstable if not used properly. The EnableCancelKey command will enable you to do special processing when the User presses the 'Esc' key. The built in error module will have a value of 'Err.Number = 18'.

    Please Login or Register  to view this content.
    It is a best practice to declare all variables. If you misspell a variable in your code, VBA will silently assume it is a Variant variable and go on executing with no clue to you that you have a bug. Go to the VBA development window, click Tools, Options, and check "Require Variable Declaration." This will insert the following line at the top of all new modules:
    Please Login or Register  to view this content.
    This option requires all variables to be declared and will give a compiler error for undeclared variables.

    Lewis

  3. #3
    Registered User
    Join Date
    07-22-2017
    Location
    Wisconsin, US
    MS-Off Ver
    2016
    Posts
    4

    Re: Trigger to exit macro based on user input of Designated Key.

    Thank you so much Lewis! I'm obviously fairly new to macro "writing," although I've recorded many of them and then tweaked. I have a lot of learning to do but am amazed with the power at our disposal when they are done well. I'm overwhelmed with how helpful so many of you are on this site -- across so many different types of questions. I will take a look at this and give it a try. (Thanks, particularly, for proactively guarding me on declaring the variables)

+ 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. [SOLVED] Trigger Event That Only Starts From User Input
    By dougman824 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-14-2017, 04:40 PM
  2. user defined macro to replace certain characters in a string based on user input
    By whatappears in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 12-17-2012, 06:25 PM
  3. [SOLVED] Count Rows Based On User Input & Trigger Print Of Pages Out Of Count Result
    By wliang in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-09-2012, 02:45 AM
  4. Automatic Formula Input Based on User Input Using a Macro
    By Drayloc in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-03-2012, 12:10 PM
  5. Macro that graphs based on user input
    By iambalrog in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-05-2006, 04:56 AM
  6. How to trigger a macro on a worksheet on the event of user-input?
    By fiven in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-20-2005, 11:38 PM
  7. How to auto-trigger a macro on a worksheet on the event of user-input?
    By fiven in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-19-2005, 11:23 AM

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