处理取消InputBox以选择范围 [英] Handle cancellation of InputBox to select range
问题描述
我有以下代码:
dim selectRange as Range
Set selectRange = Application.InputBox("Select your range", "Hello", , , , , , 8)
当用户选择取消InputBox提示,它返回错误的Object未设置
。
When a user chooses Cancel the InputBox prompt, it returns error of Object not set
.
我已经尝试使用Variant变量类型但我不能处理它。在取消的情况下,它返回 False
,同时在选择范围的情况下,返回InputBox的范围。
I have tried to use a Variant variable type but I can't handle it. In case of cancelling, it returns False
, meanwhile in case of selecting a range, it returns Range of InputBox.
如何避免这个错误?
推荐答案
使用输入框选择范围时,这是一个问题。 Excel返回范围之前返回一个错误,当您按取消时,它会携带此错误。
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
这篇关于处理取消InputBox以选择范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!