Excel 运行时错误';91';。对象变量或未设置块变量
我有一个Excel宏,它已经运行了一年多了,直到昨天我收到一个错误Excel 运行时错误';91';。对象变量或未设置块变量,excel,vba,excel-2010,Excel,Vba,Excel 2010,我有一个Excel宏,它已经运行了一年多了,直到昨天我收到一个错误运行时错误'91'。对象变量或块变量未设置。我可以确认所有的表格都存在。感谢您对故障排除的建议。我已经突出显示了发生错误的行 Sub Insert_Last_to_Input() Dim dateBaltic As Date Dim rngFFA As Range Dim longLastRowNo As Long Dim longBaltic, longFEI, longMB As Long
运行时错误'91'。对象变量或块变量未设置
。我可以确认所有的表格都存在。感谢您对故障排除的建议。我已经突出显示了发生错误的行
Sub Insert_Last_to_Input()
Dim dateBaltic As Date
Dim rngFFA As Range
Dim longLastRowNo As Long
Dim longBaltic, longFEI, longMB As Long
dateBaltic = Sheets("Input").Range("B2").Value
Sheets("Baltic FFA").Select
With ActiveSheet
longLastRowNo = .Range("A:A").Find(What:=dateBaltic, LookIn:=xlValues).Row
'**Error above:"Run-time error '91'. Object variable or With block variable not set"**
Set rngFFA = .Range("B" & longLastRowNo & ":F" & longLastRowNo)
End With
End Sub
问题似乎是由于无法“找到”文本,因此无法将其行返回给您。
也就是说,在活动工作表列A中找不到“输入”工作表单元格“B2”中的文本。谢谢大家。我找到了答案,但确实很奇怪 首先,问题在于“.Find”。Excel找不到我想要的值,因为列宽太小,无法显示全文!见下图1 在上面的示例中,longLastRowNo的正确值为213。所以我做的是增加列宽,这样可以显示全文。见下图2 请注意,当我加宽列宽时,“.Find”能够找到正确的行号并将其分配给longLastRowNo,即213 所以问题解决了。非常感谢大家 找麻烦的方法 你的问题似乎已经解决了,但在内心深处,你必须知道它不是。您可以使用以下快速修复:
longLastRowNo = .Range("A:A").Find(What:=dateBaltic, LookIn:=xlFormulas).Row
但我强烈建议您使用正确的方法,例如:
With WorkSheets("Baltic FFA")
Dim cel As Range
Set cel = .Range("A:A").Find(What:=dateBaltic, _
LookIn:=xlFormulas, _
LookAt:=xlWhole)
If Not cel Is Nothing Then
' Cell found.
longLastRowNo = cel.Row
Set rngFFA = .Range("B" & longLastRowNo & ":F" & longLastRowNo)
Else
' The cell could not be found.
MsgBox "Could not find..."
Exit Sub
End With
End With
制作行表格(“波罗的海FFA”)。选择冗余(不需要)
阅读有关如何避免在此中使用Select
。您需要检查.Find
是否返回了内容。这就是一个例子。查看该链接中的第1节如果不是aCell,则顺便说一句,在stackoverflow中此类问题已经被问了无数次。在一行中声明多个变量,例如键入Long
是这样做的:Dim longbortic As Long,longFEI As Long,longMB As Long
。在您的代码中,前两个声明为Variant
。