Results 1 to 4 of 4

Thread: Inputbox capture OK and Cancel without any data entered
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Nov 2008
    Posts
    1,011
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Inputbox capture OK and Cancel without any data entered

    I'm missing something here. I want the user to enter a number. If they don't enter a number, but instead they enter text, they get "Number is not valid" and the value entered is highlighted for the user to try again.

    If they select cancel they would get
    "You did not enter a number. Please try again and enter a number". How do I capture if they select Ok and nothing entered?

    Also, I'm running this thru a Button (Form Control) on the spreadsheet, but even with the VBE closed, the msgbox does not popup immediately. I have to click on the Excel icon on my taskbar to get the msgbox. I've never seen this before. What am I missing?

    Code:
    Sub CreateInputBoxMethodDataType()
        Dim myNumberResponse As Variant
        myNumberResponse = Application.InputBox(Prompt:="Enter a number", Type:=1)
        
        If myNumberResponse = False Then
            MsgBox "You did not enter a number.  Please try again and enter a number"
        End If
            Exit Sub
        
    End Sub

  2. #2
    MrExcel MVP shg's Avatar
    Join Date
    May 2008
    Location
    The Great State of Texas
    Posts
    21,607
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    14 Thread(s)

    Default Re: Inputbox capture OK and Cancel without any data entered

    Not sure I follow, but perhaps:

    Code:
    Sub x()
      Dim sInp    As String
     
      sInp = InputBox("Enter something")
     
      If StrPtr(sInp) = 0 Then
        MsgBox "User pressed Cancel"
      ElseIf Len(sInp) Then
        MsgBox "User entered """ & sInp & """ and pressed OK"
      Else
        MsgBox "User entered nothing and pressed OK"
      End If
    End Sub
    IsNumeric() will tell you if sInp is coercible to a number (Double).

  3. #3
    Board Regular
    Join Date
    Nov 2008
    Posts
    1,011
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Inputbox capture OK and Cancel without any data entered

    Thank you shg. This works great for my needs.

  4. #4
    MrExcel MVP shg's Avatar
    Join Date
    May 2008
    Location
    The Great State of Texas
    Posts
    21,607
    Post Thanks / Like
    Mentioned
    0 Post(s)
    Tagged
    14 Thread(s)

    Default Re: Inputbox capture OK and Cancel without any data entered

    You're welcome.

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

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