我有一个问题,当我尝试调试此编码时,它将出现
运行时错误91;对象变量或未设置块变量
,它也会在此行突出显示
Range("L8").Value = Cells(2, FindMe.Column)
。
我可以知道是什么错误吗?
Sub Macro1()
'
' Macro1 Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
Set Searchme = Range("L9")
Set FindMe = Range("A2:G126").Find(What:="Searchme", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
Range("L8").Value = Cells(2, FindMe.Column)
Range("A1:G126").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"L8:L9"), CopyToRange:=Range("N8:T8"), Unique:=False
End Sub
答案 0 :(得分:2)
您要查找的值在该范围中不存在。
尝试将代码包装在If条件周围,以检查find是否返回任何内容!
Sub test()
Set Searchme = Range("L9")
Set FindMe = Range("A2:G126").Find(What:="Searchme", LookIn:=xlValues, LookAt _
:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If FindMe Is Nothing Then
MsgBox "No value found."
'or you can negate the condition like "If Not" if you want to continue and remove the else part.
Else
Range("L8").Value = Cells(2, FindMe.Column)
Range("A1:G126").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range( _
"L8:L9"), CopyToRange:=Range("N8:T8"), Unique:=False
End If
End Sub