Application.InputBox - "Cancel" button

KerryFSA

New Member
Joined
Dec 5, 2018
Messages
49
I am having trouble coding a macro in the case where the response to an InputBox request is the "Cancel" button.
As I understand it, the cancel button returns the value "False". The following code results in "Type Mismatch" error 13 at the "If Q = False" line. Q has been defined as a variant. In short, if "Cancel" is clicked, I want he sub to end.

Q = Application.InputBox(Prompt:="Please enter a number from 1 to 3 to indicate how you want the lists sorted:" & vbCr & _
"1 - alphabetically by name;" & vbCr & _
"2 - alphabetically city;" & vbCr & _
"3 - numerically by location number" & vbCr & _
" After number entered, click 'OK.'", Title:="SORT CRITERIA", Type:=1)
' If "cancel" is clicked:
If Q = False Then
ActiveWorkbook.Sheets(sh).Range("D10").Select
Application.CutCopyMode = False
Exit Sub
End If
Other code and the sort routines follow.

Thanks very much for your help.
 
Hi, KerryFSA
I can’t test it on Mac, but if clicking cancel will throw an error why not using an error handling, something like:
On Error GoTo skip:

And put “skip: “ in the end of the code.
 
Upvote 0

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
As I don't have a Mac, I can't test anything, but there are a number of differences between VBA on a PC & on a Mac.
The prompt not appearing may simply be a bug in the Mac version.
 
Upvote 0
The type argument of the InputBox is 1. That means its expecting a numeric entry and will be outputting a number.

If the user presses Cancel, Application.Inputbox("prompt", type:=1) will return the numeric equivalent of False, ie. 0.
 
Upvote 0
With the code from post#7 The message box return "False"
 
Upvote 0
After much trial and error, I believe I have found the problem: the lines of text in my InputBox were simply too long to fit in the dialogue box and the InputBox was not even executing. For posting here, I used shorter lines to make the post easier to read. If I am correct, I apologize for wasting your time. With shorter lines, the output is "false" for cancel and I could alter the coding to end the sub. However, the apparent Mac quirk remains as noted above and i will not be able to use "Application InputBox" on the Mac. However, it may work in the Windows version of my program. Thank you all very much for replying; your answers will be useful in any event.
 
Upvote 0
Glad you've manage to sort it (after a fashion) & thanks for the feedback
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,772
Messages
6,126,803
Members
449,337
Latest member
BBV123

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