【发布时间】:2014-02-01 10:09:01
【问题描述】:
我正在运行以下宏以通过 MS XL 2003 中的数据库执行基本的“FindHighlight”功能
查找功能完美运行,但我遇到的问题是,当使用“ClearHighlight”时(在进行搜索之前),我得到运行时错误 91 -“对象变量或未设置块变量”
我知道在使用此功能之前我需要完成搜索,但使用该工具的其他人可能不需要 - 我想知道是否有办法防止出现此警报? (VBA 初学者!!)
谢谢!
Dim FoundRange As Range
Sub FindHighlight()
Dim tempCell As Range, Found As Range, sTxt As String
sTxt = InputBox("Search string")
If sTxt = "False" Then Exit Sub
Set Found = Range("A1")
Set tempCell = Cells.Find(what:=sTxt, After:=Found, SearchDirection:=xlNext, MatchCase:=False)
If tempCell Is Nothing Then
MsgBox prompt:="Not found", Title:="Finder"
Exit Sub
Else
Set Found = tempCell
Set FoundRange = Found
End If
Do
Set tempCell = Cells.FindNext(After:=Found)
If Found.Row >= tempCell.Row And Found.Column >= tempCell.Column Then Exit Do
Set Found = tempCell
Set FoundRange = Application.Union(FoundRange, Found)
Loop
FoundRange.Interior.ColorIndex = 6
FoundRange.Font.ColorIndex = 3
End Sub
Sub ClearHighlight()
FoundRange.Interior.ColorIndex = xlNone
FoundRange.Font.ColorIndex = xlAutomatic
End Sub
【问题讨论】:
标签: vba find runtime-error highlight