繁体   English   中英

VBA-范围:“运行时错误'91'

[英]VBA - Range: "Run-time error '91'

如果未保存工作簿并关闭并重新打开,则会出现以下错误

“运行时错误'91':对象变量或不设置带块”

在其他地方,我有完全相同的代码(只是字符串的名称不同),有时它会给我相同的错误,直到我保存关闭并重新打开并重新运行。 之后,代码运行平稳。

关于如何避免该错误的任何想法? 你以前有这个问题吗?

Dim fal As Excel.Worksheet
Set fal = wb.Sheets("Falancs")

Dim x As String
x = "F_1 ="

Dim cc As Integer ' The column as an integer (cc = 1,2,3...)
cc = fal.UsedRange.Find(x).Column

错误在最后一行,“(x)”是...

如果找不到x值, .FindNothing返回Nothing ,因此您应该检查一下:

Dim fal As Excel.Worksheet
Set fal = wb.Sheets("Falancs")

Dim x As String
x = "F_1 ="

Dim cc As Integer ' The column as an integer (cc = 1,2,3...)
Dim res As Range

Set res = fal.UsedRange.Find(x)
If Not res Is Nothing Then
    cc = res.Column
Else
    MsgBox "Value " & x & " not found"
    Exit Sub
End If

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM