excel - Range.Find 在查找失败时产生错误
问题描述
这是两行简单的代码,用于在列表中查找 6 位数字的实例。如果数字存在,一切正常,但如果不存在,我会收到错误“对象变量或未设置块变量”
代码行是
COL = Cells(5, "A").Text
RowFound = Sheets("Master List").Columns("A").Find(What:=COL).Row
单元格格式为列中的文本和搜索数据单元格中的文本
对此的任何建议都会有所帮助并非常感谢
解决方案
这就是 VBA 的工作方式。VBA 不能从不存在的范围返回行。
请尝试下一个方法:
Sub testFindFail()
Dim c As Range, RowFound As Long, COL As String
COL = cells(5, "A").text
Set c = Sheets("Master List").Columns("A").Find(What:=COL) 'it will be good to also use some other parameters.
If c Is Nothing Then Exit Sub 'or do something else
RowFound = c.row
Debug.Print RowFound
End Sub
推荐阅读
- nginx - Nginx 不代理仅针对主机名的请求,而是代理具有路径的请求
- javascript - 添加唯一数据属性以使用 webpack 响应 dom 元素进行开发
- mysql - 取消对用户的访问是否会影响当前连接?
- c++ - 在计算着色器的无序访问视图中写入 TextureCube
- android - 是否可以在 Android 主屏幕小部件中向 imageView 添加自定义可绘制对象?
- angular - 如何使用 Angular 7 将文件上传到 Spring Boot?
- python - 如何在函数的同一行上打印数组的一部分而没有空间?
- flutter - Dart 类 'String' 没有实例方法 'cast'
- c# - 全局异常处理程序 ASP.Net Core MVC
- reactjs - Reducer 更新不会触发组件生命周期方法