application.inputbox 0-value VS CANCEL

L

Legacy 146544

Guest
Hi all,

I'm using the application.inputbox function to get a value from the user (as currency). If the user clicks cancel, the procedure should end. But it should be possible for the user to input the numerical value 0 and then click OK.

How can I know if a 0 was entered or if cancel was clicked?
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Hi

Use a Variant. If the user presses Cancel the InputBox() returns False.

Try:

Code:
Sub Test()
Dim vRet As Variant
 
vRet = Application.InputBox("Enter Value", Type:=1)
MsgBox vRet
End Sub
 
Upvote 0
Thx pgc, but didn't get it to work yet.

I tried the code below and in both cases
-pressing CANCEL
-typing 0 and pressing OK
the CASE FALSE is executed

Any way to differentiate between them?

Sub Test()
Dim vRet As Variant

vRet = Application.InputBox("Enter Value", Type:=1)
MsgBox vRet
Select Case vRet
Case False
MsgBox "FALSE"
Case 0
MsgBox "0"
End Select

End Sub
 
Upvote 0
You can test if the return value is boolean, for ex.:

Code:
Sub Test()
Dim vRet As Variant
 
vRet = Application.InputBox("Enter Value", Type:=1)
 
If (VarType(vRet) = vbBoolean) And (vRet = False) Then
        MsgBox "False"
ElseIf vRet = 0 Then
        MsgBox "Zero"
Else
        MsgBox vRet
End If
End Sub

Remark: you just need an If (boolean false) and an Else (numeric value), I divided it in 3 branches just to test the return value False vs. 0.
 
Upvote 0
That's it. Thank you very much.

....and learned something. Was until now always 'not getting exactly what I needed' with the IsNumeric and IsDate functions when evaluating input.

From now on I'll use the VarType() function, thx man.
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
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