Consulting

Results 1 to 8 of 8

Thread: Sleeper: Input Box - Exit Sub on Cancel

  1. #1
    Administrator
    2nd VP-Knowledge Base
    VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location

    Sleeper: Input Box - Exit Sub on Cancel

    What's the code to exit a sub when you press Cancel for an Input box?

    Just a short example:
    Sub InputBox() 
        Dim WSName As String 
        WSName = Application.InputBox("What is the Sheet Name?", Type:=2) 
        If WSName = "" Then 
            MsgBox "Sheet will not be created. There is no name for it.", _ 
            vbOKOnly + vbExclamation, "No new sheet" 
            Exit Sub 
        End If 
    Msgbox "Still Running"
    End Sub
    If you press cancel the macro continues, and I want it to exit the sub.

    Thanks in advance!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  2. #2
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    Check out this Kb Entry for an example on how to determine if Cancel was pressed or not.

  3. #3
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    That works for VBA's Input box function, but what about Excel's Input box method?

    If you change the code to this:

    Sub InputBoxNew()
    Dim Temp$
    Temp = Application.InputBox("Enter something here:", "Inputbox", Type:=2)
    If StrPtr(Temp) = 0 Then
    MsgBox "You pressed Cancel!" 'Option 1
    Else
    If Temp = "" Then
    MsgBox "You entered nothing and pressed OK" ' Option 2
    Else
    MsgBox Temp 'Option 3
    End If
    End If
    End Sub
    It doesn't work, you end up with Option 3 and Temp is False.

    I was trying to change the code to correct it, but I'm having a hard time figuring it out.




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  4. #4
    Site Admin
    Jedi Master
    VBAX Guru Jacob Hilderbrand's Avatar
    Joined
    Jun 2004
    Location
    Roseville, CA
    Posts
    3,712
    Location
    It works for me. False is when you press Cancel.

  5. #5
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Quote Originally Posted by DRJ
    It works for me. False is when you press Cancel.
    Yeah, but when you press cancel the KB Entry says that you're supposed to get a MsgBox saying "You pressed Cancel!" (Option 1 in KB Entry), not a MsgBox that says "False" (which would be option 3 in the KB Entry).

    Right?




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  6. #6
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Okay, I got it.

    I had to get rid of the StrPtr function and change 0 to "False"
    Like this:


    Using Excel's InputBox method:

    Sub InputBoxNew() 
    Dim Temp As String 
    Temp = Application.InputBox("Enter something here:", "Inputbox", Type:=2) 
    If Temp = "False" Then 
    MsgBox "You pressed Cancel!" 'Option 1
    Else 
    If Temp = "" Then 
    MsgBox "You entered nothing and pressed OK" ' Option 2
    Else 
    MsgBox Temp 'Option 3
    End If 
    End If 
    End Sub

    Using VBA's InputBox function (from KB Entry):

    Sub InputBoxNew() 
    Dim Temp As String 
    Temp = InputBox("Enter something here:", "Inputbox") 
    If StrPtr(Temp) = 0 Then 
    MsgBox "You pressed Cancel!" 'Option 1
    Else 
    If Temp = "" Then 
    MsgBox "You entered nothing and pressed OK" ' Option 2
    Else 
    MsgBox Temp 'Option 3
    End If 
    End If 
    End Sub
    That's strange how it works differently for each one, huh

    Maybe someone should state that in the KB Entry.



    Anyway, thanks DRJ works great now!




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

  7. #7
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    See this recent thread on the same topic

    http://vbaexpress.com/forum/showthread.php?t=5211
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  8. #8
    Administrator
    2nd VP-Knowledge Base VBAX Master malik641's Avatar
    Joined
    Jul 2005
    Location
    Florida baby!
    Posts
    1,533
    Location
    Interesting...

    Why did excel even bother with two different types of Input boxes anyway?




    New to the forum? Check out our Introductions section to get to know some of the members here. Feel free to tell us a little about yourself as well.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •