处理取消输入框以选择范围 [英] Handle cancellation of InputBox to select range

查看:13
本文介绍了处理取消输入框以选择范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下一段代码:

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

当用户选择取消输入框提示时,返回未设置对象错误.

When a user chooses Cancel the InputBox prompt, it returns error of Object not set.

我曾尝试使用 Variant 变量类型,但无法处理.取消时返回False,同时选择范围时返回InputBox的Range.

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.

我怎样才能避免这个错误?

How can I avoid this error?

推荐答案

这是在使用输入框选择范围时出现的问题.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 

这篇关于处理取消输入框以选择范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆