vbcancel with inputbox

Stephanie0205

Board Regular
Joined
Apr 14, 2005
Messages
152
Is it possible to determine if someone hits the cancel button on an inputbox? As far as I can tell, the vbcancel option is not available.
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
What type of inputbox are you using?

Is it the VBA one or are you using Application.Inputbox?

Can we see your code?
 
Upvote 0
You can check for vbNullString.

Ex:

Code:
'...
Dim strResponse As String
strResponse = InputBox("Please enter a value:")
If strResponse = vbNullString Then MsgBox "You pressed Cancel!"
'...
 
Upvote 0
CANCEL BUTTON DEMO... :-D :wink:

Code:
Public Sub Demo()
Reply = Application.InputBox("blah blah blah", "Title here")

If Reply = False Then

    MsgBox ("CANCEL BUTTON WAS PRESSED")

End If
End Sub

Code:
Public Sub Demo2()
Reply = Application.InputBox("blah blah blah", "Title here")

If Reply = False Then Exit Sub

    MsgBox ("CANCEL BUTTON WAS NOT  PRESSED")

End Sub

you must use "application" in front of the word "inputbox" :wink: ... its the difference between using the inputbox function and method.
 
Upvote 0
As far as I know hitting Cancel will result in a "nothing-result"

answer = Inputbox ....
If answer Is Nothing Then ...

kind regards,
Erik

(or do I miss something, Norie ?)

EDIT: posted two replies, but deleted the second, I didn't see all posts when typing my messages
 
Upvote 0
It depends what type of InputBox is used.

By the way your code generates an Object Required error if you press cancel.
 
Upvote 0
That's because the InputBox method does not return Nothing, it handles Strings.
 
Upvote 0
Well, I was using this code...
Code:
Dim dt As Date
dt = InputBox("Please enter date.")
If dt = vbNullString Then
...
and when the user hit cancel he would get a type mismatch error. So I changed the code according to Nimrod's suggestion.
Code:
Dim dt As Date
dt = Application.InputBox("Please enter date.")
If dt = False Then
...

Thanks for all your help. It is much appreciated.
 
Upvote 0
Stephanie0205 said:
Is it possible to determine if someone hits the cancel button on an inputbox? As far as I can tell, the vbcancel option is not available.
Some people only use the Application.InputBox method when they want to trap cancel but it is do-able with the function InputBox as an example below using strptr:


Sub CancelExample()
Dim ans1$
ans1 = InputBox("Please enter the name:", "Name")
Select Case True
Case StrPtr(ans1) = 0
MsgBox "You hit Cancel.", 48, "Entry cancelled."
Exit Sub
Case Len(ans1) = 0
MsgBox "You hit OK but entered nothing.", 48, "Entry scuttled."
Exit Sub
Case Else
MsgBox "You entered ''" & ans1 & "''."
End Select
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,315
Messages
6,171,412
Members
452,399
Latest member
oranges

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