VBA: Using Input Box Cancel Button to Exit Sub

  • I have a sub routine that starts with a series of Input boxes. It would be very helpful if I could figure out how to exit the sub routine when the cancel button on any of the Input boxes is clicked. Does anyone have ideas?

  • Yes, but you could perhaps get around this by having a default entry for each input box of " " (a space) and then after you checked for the cancel button you could check for the space and remove it or do whatever you would normally do with a blank entry.

  • Re: VBA: Using Input Box Cancel Button to Exit Sub


    Try to use like this:



    sub inputbox_verification()


    text=inputbox("type the text")


    if StrPtr(text)=0 then
    'if it entenrs here then the user pressed "cancel"
    endif


    if text=""
    'if enters here the user left in blank
    end if


    if text<>""
    'if enters here the user entered some text
    end if


    end sub

  • Re: VBA: Using Input Box Cancel Button to Exit Sub


    titarelli,


    excellent use of the strptr function there. Might I offer that a Select Case could tidy the code somewhat


  • Re: VBA: Using Input Box Cancel Button to Exit Sub


    StrPtr does not seem to return 0 if using application.inputbox. Any other way to test for cancel using application.inputbox?

  • Re: VBA: Using Input Box Cancel Button to Exit Sub


    Don-NS,


    Please do not post your question in threads started by others - - this is known as thread hijacking.
    Always start a new thread for *YOUR* question. If you find it helpful to clarify your needs you can include a link to this ( or any other ) thread.


    When starting a new thread be sure to give it a search-friendly title that aptly describes your need.
    The better your title define the thread the more relevant the results returned when searching.

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!