Inputbox capture OK and Cancel without any data entered

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,366
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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:
[FONT=Verdana,Arial,Tahoma,Calibri,Geneva,sans-serif]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[/FONT]
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
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).
 
Upvote 0
Thank you shg. This works great for my needs.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top