Exit Sub if user presses cancel button in inputbox or when it is empty

ayama51

New Member
Joined
Nov 8, 2017
Messages
3
Hi,

I have an input box that asks user to select a range of cells, then copies the cells and opens a new sheet.

I want to make sure if the user clicks on Cancel it exits the sub and if user clicks ok and the box is empty it prompts the user with a msgbox and else it continues with the macro.

any expert help is welcomed.

this is what i have;
-----------------------------------
Sub RangeSelectionPrompt()


Dim rng As Range

Set rng = Application.InputBox("Select Range, (Client Data): Column A - E only", "Obtain Range Object", Type:=8)


ActiveSheet.Range(rng.Address).Select


Call NewSheet


End Sub
-------------------------------
thanks,
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
Try...

Code:
[font=Courier New][color=darkblue]Sub[/color] RangeSelectionPrompt()

    [color=darkblue]Dim[/color] rng [color=darkblue]As[/color] Range
    
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]Resume[/color] [color=darkblue]Next[/color]
    [color=darkblue]Set[/color] rng = Application.InputBox("Select Range, (Client Data): Column A - E only", "Obtain Range Object", Type:=8)
    [color=darkblue]On[/color] [color=darkblue]Error[/color] [color=darkblue]GoTo[/color] 0
    
    [color=darkblue]If[/color] rng [color=darkblue]Is[/color] [color=darkblue]Nothing[/color] [color=darkblue]Then[/color] [color=darkblue]Exit[/color] [color=darkblue]Sub[/color]
    
    ActiveSheet.Range(rng.Address).Select
    
    [color=darkblue]Call[/color] NewSheet

[color=darkblue]End[/color] [color=darkblue]Sub[/color][/font]

Hope this helps!
 
Upvote 0
Thanks Domenic,

It worked but a follow up question as you now what i am dealing with.
when user clicks cancel its exiting the sub which is working fine and i will need a msgbox if the user click ok when the input box is empty.

thanks,
 
Upvote 0
As far as I know, I don't think it's possible. But since the dialog box that appears isn't very useful, and maybe confusing to the user, you can prevent it from appearing by setting the DisplayAlerts property to False at the beginning of the code...

Code:
Application.DisplayAlerts = False

In this case, nothing will hapen when pressing Ok without making a selection.
 
Last edited:
Upvote 0
It worked but a follow up question as you now what i am dealing with.
when user clicks cancel its exiting the sub which is working fine and i will need a msgbox if the user click ok when the input box is empty.
There does not seem to be anyway to trap the user clicking "OK" when the "Type" argument is 8 (range) and the input field is empty or contain text, but we can shut off the non-trappable error message that Application.InputBox issues when the user does that. The following code ends up requiring the user to either click "OK" when there is something in the input field or click "Cancel"... the user will have no other option as the "OK" button will do nothing when the input field is empty or the user attempts to enter a non-range.
Code:
[table="width: 500"]
[tr]
	[td]Sub RangeSelectionPrompt()
  Dim Rng As Range
  On Error Resume Next
  Application.DisplayAlerts = False
  Set Rng = Application.InputBox("Select Range, (Client Data): Column A - E only", "Obtain Range Object", Type:=8)
  Application.DisplayAlerts = True
  On Error GoTo 0
  If Rng Is Nothing Then Exit Sub
[COLOR="#008000"]  '
  '  Rest of your code goes here
  '
[/COLOR]End Sub[/td]
[/tr]
[/table]
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,228
Messages
6,170,875
Members
452,363
Latest member
merico17

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