Results 1 to 3 of 3

Thread: How To React To The Cancel Button in a VB (not Application) InputBox

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Forum Guru Rick Rothstein's Avatar
    Join Date
    Feb 2012
    Posts
    659
    Rep Power
    13

    How To React To The Cancel Button in a VB (not Application) InputBox

    This posting is "short and sweet" as I think the below code snippet is self-explanatory (even though the StrPtr function may be new to you)...
    Code:
    Sub TestMe()
      Dim Answer As String
      '....
      '....
      Answer = InputBox("Tell me something")
      If StrPtr(Answer) = 0 Then
        MsgBox "The user clicked Cancel, so we will exit the subroutine now."
        Exit Sub
      ElseIf Len(Answer) = 0 Then
        MsgBox "The user clicked OK without entering anything in the InputBox!"
      Else
        MsgBox "The user entered the following..." & vbLf & vbLf & Answer
      End If
      '....
      '....
    End Sub
    Simply replace the MsgBoxes with any code you might want to execute for the stated outcome. To get a better feel for what is going on, copy/paste the above code into a general Module (Insert/Module from the VB editor menu bar) and run it.... first click the OK button without entering anything, then type something in and click the OK button, then finally click the Cancel button (with or without text in the type-in field).
    Last edited by Rick Rothstein; 05-30-2014 at 02:26 AM.

Similar Threads

  1. InputBox OK and Cancel Button Problem
    By mackypogi in forum Excel Help
    Replies: 5
    Last Post: 05-30-2014, 12:20 AM
  2. Replies: 2
    Last Post: 11-02-2013, 04:17 PM
  3. Replies: 4
    Last Post: 07-10-2013, 04:35 AM
  4. Application.Caller
    By Transformer in forum Familiar with Commands and Formulas
    Replies: 0
    Last Post: 05-01-2013, 12:32 PM
  5. Print VBA cancel command not working
    By Tony in forum Excel Help
    Replies: 1
    Last Post: 02-07-2013, 07:09 PM

Posting Permissions

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