我在下面有这个宏,非常适合查找数字并确认它是否正确(然后突出显示)。首先它会询问您要查找的号码,然后输入号码并点击“确定”。然后,如果它找到了号码,那么你将有机会说是否或取消(以防它们是你正在寻找的多个号码而不是正确的号码)。如果您点击是,则会突出显示该单元格。
问题:
任何帮助将不胜感激!谢谢!
Sub find_highlight()
Dim w As Variant
Dim FoundCell As Range
Dim ans As String
Do
w = InputBox("What to find?")
Cells.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
Select Case MsgBox("Is this the number?", vbYesNoCancel)
Case vbNo
Case vbYes
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Case vbCancel
Exit Do
End Select
Loop
End Sub
答案 0 :(得分:1)
你真的应该把它分成多个帖子。请发一个问题。
至于您所看到的错误:您必须检查错误情况。
Sub find_highlight()
Dim w As Variant
Dim FoundCell As Range
Dim ans As String
Do
w = InputBox("What to find?")
On Error Resume Next
thisRng1 = ActiveCell.Address
Cells.Find(What:=(w), After:=ActiveCell, LookIn:=xlFormulas, _
LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
MatchCase:=False).Activate
thisRng2 = ActiveCell.Address
If thisRng1 = thisRng2 Then
MsgBox "Value Not Found"
End If
On Error GoTo 0
Select Case MsgBox("Is this the number?", vbYesNoCancel)
Case vbNo
Case vbYes
With Selection.Interior
.ColorIndex = 6
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
End With
Case vbCancel
Exit Do
End Select
Loop
End Sub
答案 1 :(得分:1)
以下代码:
=5+4
,你会想要匹配它,如果用户正在搜索{{1} },而不是他们正在搜索9
)4
未发生匹配时发生匹配的单元格时,通过在尝试执行测试以确保匹配之前不尝试Activate
来发生错误发生
Activate