Announcement

Collapse
No announcement yet.

VBA: Using Input Box Cancel Button to Exit Sub

Collapse
This topic is closed.
X
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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?

  • #2
    If the cancel button is clicked then InputBox returns a zero length string, so just check on that. For example,
    x = InputBox("Input Something")
    If x = "" Then Exit Sub
    Please do not U2U (private message) me directly for questions that should be posted to the forum; any such U2Us will be ignored.

    Comment


    • #3
      There may be input boxes that are left blank when the OK button is clicked will that also exit the sub if I go this route?

      Comment


      • #4
        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.
        Please do not U2U (private message) me directly for questions that should be posted to the forum; any such U2Us will be ignored.

        Comment


        • #5
          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

          Comment


          • #6
            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

            Code:
            Dim response As Variant
                
                response = InputBox("Prompt", "Title")
                Select Case StrPtr(response)
                    Case 0
                        'OK not pressed
                        Exit Sub
                    Case Else
                        'OK pressed
                        'Carry on your routine, variable response contains the InputText
                End Select

            Comment


            • #7
              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?

              Comment


              • #8


                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.
                AAE
                ----------------------------------------------------

                Forum Rules | Message to Cross Posters | How to use Tags

                Comment

                Working...
                X