Exit SUB if CANCEL is pressed in Input Box

S.H.A.D.O.

Well-known Member
Joined
Sep 6, 2005
Messages
1,915
Good morning,

I have the code:

Code:
    Dim n As Integer

    n = InputBox("Total numbers to be drawn from?", "Combinations")

What do I need to add so that if the user clicks Cancel it will exit the Sub please.
I have n dimmed as integer as it is used as a value in the code.

I have tried using things like:

Code:
If n = 0 Then Exit Sub

If n = "" Then Exit Sub

Thanks in advance.
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
Try this :
Code:
    Dim n As String
    n = InputBox("Total numbers to be drawn from?", "Combinations")
    If StrPtr(n) = 0 Then Exit Sub
 
Upvote 0
Thanks for the reply Jaafar Tribak,

Try this :
Code:
    Dim n As String
    n = InputBox("Total numbers to be drawn from?", "Combinations")
    If StrPtr(n) = 0 Then Exit Sub
Won't changing the varable to String muck up the fact that it is used as an Integer for the combinations or slow the code down?

Thanks again.
 
Upvote 0
Good afternoon,

I have come up with the following...

Code:
    Dim n As Integer
    n = Application.InputBox("Total numbers to be drawn from?", "Combinations", 0)
    If n = 0 Then Exit Sub
...which works by basically just adding Application.InputBox

I also tried:-

Code:
    n = Application.InputBox("Total numbers to be drawn from?", "Combinations", 0)
    Select Case n
        Case 1
    Case 0
        Exit Sub
    End Select
...which seems to work.
 
Upvote 0

Forum statistics

Threads
1,214,980
Messages
6,122,563
Members
449,088
Latest member
Motoracer88

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