Exit sub when cancel button pressed on InputBox

marc81

New Member
Joined
Aug 3, 2017
Messages
16
Hi,

I have an Input Box to select a case based on the user input of option 1 or 2. All works fine but I want to be able to utilise the cancel button as at the moment it does nothing at all. Is there a way I can adjust the below code to exit the sub if the cancel button is pressed?

Dim Prompt As String
Dim UserResp As String
Dim UR As Integer
Prompt = "1. Depot List" & vbCrLf
Prompt = Prompt & "2. Account List"
UR = 0
While UR < 1 Or UR > 2
UserResp = InputBox(Prompt, "Would you like to run the ZT60 by Depot List or Account List?")
UR = Val(UserResp)
Wend
Select Case UR

Thanks,

Marc
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
VBA Code:
If UserResp = "" then exit sub

should do.
 
Upvote 0
Rich (BB code):
Dim Prompt      As String
    Dim UserResp    As Variant
    Dim UR          As Integer
    Prompt = "1. Depot List" & vbCrLf
    Prompt = Prompt & "2. Account List"
    UR = 0
    While UR < 1 Or UR > 2
        UserResp = InputBox(Prompt, "Would you Like To run the ZT60 by Depot List Or Account List?")
        'cancel pressed
        If StrPtr(UserResp) = 0 Then Exit Sub
        UR = Val(UserResp)
    Wend
    Select Case UR
        
    End Select

Dave
 
Upvote 0
Fantastic, thanks for such a quick response. Just added Dave's code which worked spot on.

Appreciate your help - simple when you know how :)
 
Upvote 0
Glad we could help & appreciate the feedback

edit: I should have added that StrPtr only works with the InputBox Function - the InputBox Method cancel button returns False.

Dave
 
Upvote 0

Forum statistics

Threads
1,215,055
Messages
6,122,902
Members
449,097
Latest member
dbomb1414

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