+ Reply to Thread
Results 1 to 7 of 7

Cancel Inputbox not stoping Sub from excuting

  1. #1
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Cancel Inputbox not stoping Sub from excuting

    I have a Inputbox
    Please Login or Register  to view this content.
    when I click cancel I check for it with
    Please Login or Register  to view this content.
    When I click cancel the msgbox executes and says"Canceled!" but I do not Exit the sub all the code following executes

    Thanks

  2. #2
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Cancel Inputbox not stoping Sub from excuting

    Hi,

    If you've called that procedure could from another procedure then exiting the procedure will return you to the calling procedure and carry on. Use instead of Exit Sub, End.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

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

    Re: Cancel Inputbox not stoping Sub from excuting

    Hi capson,

    Your question hits on one of the programming world's hot button topics. There are many ways to approach the problem. I agree that Richard's use of 'End' will work, but I disagree with it's use.

    See the attached example file, and the identical code that follow here. It uses the approach that the program always continues processing to the end of the code, even if there is an error. This is accomplished by checking for errors during each step.

    In your case, my approach is way over the top, but it gives an example of how to attempt to bulletproof your code. I include 'Location' numbers that can be used to help identify which section of code caused an error. All sections are optional, and you can implement as much or as little as you desire.

    Please NOTE that I had to add a Boolean check of the InputBox return value, to differentiate Input of 0 (ZERO), from input of Cancel.

    I hope this helps.

    Please Login or Register  to view this content.
    Lewis

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,110

    Re: Cancel Inputbox not stoping Sub from excuting

    @Lewis

    If you really want to report the location of the error, it's easier to use line numbers and Erl.
    Everyone who confuses correlation and causation ends up dead.

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

    Re: Cancel Inputbox not stoping Sub from excuting

    Rory,

    Thanks for the tip. I must admit I didn't know that Erl() existed. After a little bit of research I don't care for it, especially since it needs Line Numbers. Line Numbers can be added manually, or can be added/removed using an external freeware tool like MZTools 3.0 http://www.mztools.com/index.aspx

    I use and love MZTools, but I have hated line numbers since the late 1960s and my initial exposure to FORTRAN.

    For anyone who is interested, here are a couple of examples using Erl() to identify a divide by zero error:
    Please Login or Register  to view this content.
    Lewis

  6. #6
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,110

    Re: Cancel Inputbox not stoping Sub from excuting

    Lewis,

    I don't use it either but it seemed a lot simpler than using your variable.

  7. #7
    Forum Contributor
    Join Date
    07-11-2010
    Location
    Minneapolis, USA
    MS-Off Ver
    Excel 2016
    Posts
    308

    Re: Cancel Inputbox not stoping Sub from excuting

    Thank you all for the help.

    Lewis, I down loaded the file and will sudy it, thank you.

+ 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] inputbox cancel
    By jacobsoo in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-26-2013, 07:03 AM
  2. InputBox and Cancel
    By michaelaindia in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-24-2012, 04:32 AM
  3. [SOLVED] Cancel my Inputbox
    By nickmax1 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 08-21-2012, 11:05 AM
  4. InputBox: Cancel/X properties
    By Rick_Stanich in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 10-08-2009, 04:47 PM
  5. InputBox - Cancel
    By Lonwez in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 04-06-2005, 07:17 PM

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