使用find后出现错误91(对象变量或未设置块变量)

时间:2019-01-24 11:10:23

标签: excel vba

在关闭工作簿之前,我在VBA中有一些代码可以正常工作,但是第二天打开该工作簿时显示

  

错误91:对象变量或未设置块变量

我不明白发生了什么事。

enter image description here

Private Sub ComboBox1_Change()
Dim hq As String, asheet As Worksheet, acell As Range
Dim lrow As Double, test As String
Set asheet = ThisWorkbook.Sheets(1)
hq = ComboBox1.Value
Debug.Print hq
Set acell = asheet.Range("$Y$1:$EZ$95").Find(what:=hq, LookIn:=xlValues,     lookat:=xlPart, searchorder:=xlByRows, searchdirection:=xlNext, MatchCase:=False, searchformat:=False)
Debug.Print acell.Column
asheet.Activate
Cells(1, acell.Column).Select
Selection.End(xlDown).Select
lrow = Selection.Row + 1
ThisWorkbook.Sheets(2).Activate

End Sub

1 个答案:

答案 0 :(得分:0)

错误消息

  

错误91:对象变量或未设置块变量

意味着变量acellNothing,因为find()方法什么都没发现!使用find后,您需要检查它是否成功:

If Not acell Is Nothing Then
    'something was found
    Debug.Print acell.Column
Else
    'nothing was found
    Debug.Print "find returned nothing"
End If

要进一步调查问题Debug,请检查代码并检查变量的值。例如,变量hq