Inputbox capture OK and Cancel without any data entered

FryGirl

Well-known Member
Joined
Nov 11, 2008
Messages
1,368
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 show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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,885
Messages
6,175,178
Members
452,615
Latest member
bogeys2birdies

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