这个问题在这里已经有了答案:
Keep getting Error 91 with Excel Find function
(1 个回答)
3年前关闭。
我得到错误:Run-time error '91': Object variable or With block variable not set
当我尝试执行此代码时:
MsgBox Worksheets("Sheet2").Range("2:2").Find(Worksheets("Sheet1").Range("E5").Value, , , xlWhole)
为什么?这一行有什么问题?
编辑
如果我正在管理文本或数字,则该代码有效。但我有这个问题 日期 在
Worksheets("Sheet1").Range("E5")
和 Worksheets("Sheet2").Range("2:2")
.
最佳答案
分配 result
到一个范围并使用 .Find()
设置范围。
如果没有找到,那么 Range()
将是 Nothing
:
Sub TestMe()
Dim result As Range
With Worksheets(1)
Set result = .Range("2:2").Find("Something", LookAt:=xlWhole)
End With
If Not result Is Nothing Then
Debug.Print result.Address
Else
Debug.Print "Something is not on the second row!"
End If
End Sub
LookAt:=xlWhole
如果使用日期,参数是必须的:关于vba - Excel VBA : How to solve Run-time error '91' ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52504282/