excel - 查找特定文本、选择和突出显示
问题描述
我想找到并突出显示具有特定值的单元格。
在此示例中,我正在搜索数字 2。
该代码查找并突出显示编号为 2 的单元格,但它也突出显示编号为 22 和 23 的单元格,因为它们包含数字 2。
'Find Search Values on Sheet and Highlight
Sub Find_And_Highlight()
Dim Searchfor As String
Dim FirstFound As String
Dim Lastcell As Range
Dim FoundCell As Range
Dim rng As Range
Dim myRange As Range
Set myRange = ActiveSheet.UsedRange
Set Lastcell = myRange.Cells(myRange.Cells.Count)
Searchfor = "2"
Set FoundCell = myRange.Find(what:=Searchfor, after:=Lastcell)
'Test to see if anything was found
If Not FoundCell Is Nothing Then
FirstFound = FoundCell.Address
Else
GoTo NothingFound
End If
Set rng = FoundCell
'Loop until cycled through all finds
Do Until FoundCell Is Nothing
'Find next cell with Searchfor value
Set FoundCell = myRange.FindNext(after:=FoundCell)
'Add found cell to rng range variable
Set rng = Union(rng, FoundCell)
'Test to see if cycled through to first found cell
If FoundCell.Address = FirstFound Then Exit Do
Loop
'Highlight cells that contain searchfor value
rng.Interior.ColorIndex = 34
Exit Sub
'Error Handler
NothingFound:
MsgBox "No values were found in this worksheet"
End Sub
解决方案
推荐阅读
- excel - 如何打开 .msg 文件?
- python - 我该如何解决这个特定的“TypeError:类型'NoneType'无法序列化。” 错误?
- drake - 我已经安装了 drake 并使用 Matlab 设置了 drake,现在该怎么办?
- javascript - 大范围输入的素数总和
- angular - 带有选择的属性的角度 i18n
- javascript - 单击链接时关闭移动设备上的菜单
- docker - 我是否在容器上运行?
- python - 如何解决“模型用作中间模型但它没有模型的外键”?
- big-o - How can i solve the recursion T(n) = 5T(n/7) + logn?
- android - 自定义 CardViews 以匹配模型