excel - VBA用户定义范围并检查它是否为空
问题描述
我已经为此苦苦挣扎了一个多小时。我需要编写一个 VBA 代码,让用户在其中选择一个范围,然后在我去做其他任何事情之前检查这个选定的范围是否为空。
这是我到目前为止所拥有的:
Sub test()
Set rng= Application.InputBox("Select the range of the raw data please", Type:=8)
If Application.WorksheetFunction.CountA(Range(rng)) > 0 Then
MsgBox "do this, this and that!"
End If
End Sub
当我运行它时,我得到一个“object_Global 的方法范围失败”。我知道它可以让用户很好地选择范围,但 Range(rng) 不能正常工作。任何帮助,将不胜感激!
解决方案
您的问题是您的变量rng
是一个范围,而您试图将其包装在一个范围内,这就是它抛出错误的原因。试试这个。
Sub test()
Dim rng As Range
Set rng = Application.InputBox("Select the range of the raw data please", Type:=8)
If Application.WorksheetFunction.CountA(rng) > 0 Then
MsgBox "do this, this and that!"
End If
End Sub
推荐阅读
- javascript - JS 代码只在第一组文本的范围内换行,其余的不换行
- swiftui - 如何在 Swift 中理解这种语法 (a ? b ? c : d : c)
- c# - 使用 C# 运行带有关键字 GO 的 .sql 语句?
- apache-spark - 如果在 S3 的镶木地板文件中发现同一列的数据类型不同,AWS Glue 作业会在 Redshift 中创建新列
- python-3.x - 获取 ValueError:在尝试计算 KNN 分数时没有足够的值来解包(预期为 6,得到 5)
- python - 在不同的数据维度上评估模型
- javascript - 无法使用按钮添加 DIV 元素
- python - 捕获有效的逗号分隔数字 python 正则表达式
- java - Spring Boot 中嵌入 Flyway 的许可证是什么
- c# - 从类调用函数时抛出 NullObjectReference