Handle cancellation of InputBox to select range

This is a problem when selection a range with an inputbox. Excel returns an error before the range is returned, and it carries this error on when you press cancel.

You should therefore actively handle this error. If you don’t want anything to happen when you press cancel, you can just use the code like this:

Sub SetRange()
    Dim selectRange As Range

    On Error Resume Next
        Set selectRange = Application.InputBox("Select your range", "Hello", , , , , , 8)
    Err.Clear
    On Error GoTo 0
End Sub 

Leave a Comment