Handle cancellation of InputBox to select range

后端 未结 6 1852
小蘑菇
小蘑菇 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:28

    I have found that checking for the "Object required" error that you mentioned is one way of handling a cancel.

    On Error Resume Next
    dim selectRange as Range
    ' InputBox will prevent invalid ranges from being submitted when set to Type:=8.
    Set selectRange = Application.InputBox("Select your range", "Hello", , , , , , 8)
    ' Check for cancel: "Object required".
    If Err.Number = 424 Then
        ' Cancel.
        Exit Sub
    End If
    On Error GoTo 0
    

提交回复
热议问题