Handle cancellation of InputBox to select range

后端 未结 6 1853
小蘑菇
小蘑菇 2020-12-10 06:20

I have the following piece of code:

dim selectRange as Range
Set selectRange = Application.InputBox(\"Select your range\", \"Hello\", , , , , , 8)

6条回答
  •  情歌与酒
    2020-12-10 06:34

    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 
    

提交回复
热议问题