How to trap Cancel on Application.InputBox?

dplum

Board Regular
Joined
Nov 1, 2006
Messages
217
The following code generates an error on the InputBox command if Cancel is Clicked.

What is needed to allow the Cancel to be clicked and exit the Sub if it was clicked.

I'm checking for "False" but the program fails on the Input Command just before this check.
Code:
Sub NameSelectedCells()
    Dim vInputCells, vRefersto_Cell_Range As Range
    'type:=8 for cell references or ranges; change the Left and Top arguments to position the InputBox
    Set vInputCells = Application.InputBox(prompt:="Select cells to be named...", Left:=1, Top:=1, Type:=8)
    If vInputCells = False Then Exit Sub
    vSheet_Range = "'" & ActiveSheet.Name & "'!" & vInputCells.Address(ReferenceStyle:=xlA1)
    Set vRefersto_Cell_Range = Range(vSheet_Range)
    Do
        vRangeName = InputBox("Assign the Range[" & vSheet_Range & "]" & vbCr & _
                              "to the following Name...")
        If vRangeName = "" Then Exit Sub
        On Error Resume Next
        Names.Add Name:=vRangeName, RefersTo:=vRefersto_Cell_Range, Visible:=True
        If Err.Number = 0 Then Exit Sub
        MsgBox "RangeName... [" & vRangeName & "] is Invalid!" & vbCr & _
            "Must Not start with a Number, Must Not contain a Space, Only underscore (_) special character is allowed"
    Loop
End Sub
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
One way:

Code:
    Dim rInp As Range
    
    On Error Resume Next
    Set rInp = Application.InputBox(prompt:="Select cells to be named...", Left:=1, Top:=1, Type:=8)
    If Err.Number Then Exit Sub
    On Error GoTo 0

    ' carry on ...
 
Upvote 0
shg,

Thx... but I find this strange.

The normal "Inputbox" command allows for the Cancel button to be clicked without generating an error.

Any idea why clicking Cancel on Application.Inputbox shoudl cause an error?
 
Upvote 0
Because when you press Cancel, the InputBox method (which is not the same as the InputBox function) doesn't return a range object to set the range variable to. It might more benignly return Nothing (like Find does when it doesn't find the sought value), but it doesn't.
 
Upvote 0
Because there is no error, it returns blank

need if statement after setting variable called asd

Code:
if asd = "" then
'code when cancel hit
Exit sub
else
'code when data entered
end if
 
Upvote 0

Forum statistics

Threads
1,224,594
Messages
6,179,795
Members
452,943
Latest member
Newbie4296

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