我正在搜索excel代码,该代码在特定列中搜索特定关键字,并将其突出显示为黄色;并且能够对多个列执行此操作,每个列都有自己独特的关键字。
示例:
每次,unique关键字仅在特定列中高亮显示,即使它也可能出现在其他列中。
该守则将包括100列,从“A栏”到“简历栏”,并允许为每一栏插入一个独特的关键字。
这个是可能的吗?
在论坛中搜索时,我发现一些代码可以突出显示excel中的特定单词,但没有一个代码可以将搜索范围缩小到列,并将关键字排除在其他列之外。
为了找到一个单词并将其涂成红色,这个代码有一个类似的核心思想:
Sub colorText()
Dim cl As Range
Dim startPos As Integer
Dim totalLen As Integer
Dim searchText As String
Dim endPos As Integer
Dim testPos As Integer
' specify text to search.
searchText = "river"
' loop trough all cells in selection/range
For Each cl In Selection
totalLen = Len(searchText)
startPos = InStr(cl, searchText)
testPos = 0
Do While startPos > testPos
With cl.Characters(startPos, totalLen).Font
.FontStyle = "Bold"
.ColorIndex = 3
End With
endPos = startPos + totalLen
testPos = testPos + endPos
startPos = InStr(testPos, cl, searchText, vbTextCompare)
Loop
Next cl
End Sub
只是我需要一个黄色的高光,而不是红色。我需要excel 2016,这是excel 2010的代码。
谢谢。
发布于 2017-04-14 20:44:35
编辑:您可以高亮显示单元格或更改单元格中特定文本的字体颜色。Excel没有突出显示单元格中特定文本背景的选项。
由于您只想看到搜索到的字符串被着色,我使用了Font.ColorIndex属性和红色颜色,而不是黄色,以便于查看。
我还声明了一个数组,以便您可以根据需要输入预定义的100个唯一关键字。
如果它对你有用,请告诉我:
Sub Search_by_Column()
Dim rng As Range
Dim i As Long
Dim oldrngrow As Long
Dim myValue As String
Dim arr() As Variant
arr = Array("river", "ocean", "sea") '..... keep going till 100 keywords
For i = 1 To UBound(arr) + 1
myValue = arr(i - 1)
If myValue = vbNullString Then
End
End If
Set rng = Cells.Find(What:=myValue, After:=Cells(1, i), LookIn:=xlFormulas, LookAt _
:=xlPart, SearchOrder:=xlByColumns, SearchDirection:=xlNext, MatchCase:= _
False, SearchFormat:=False)
If rng Is Nothing Then
GoTo Skip
End If
oldrngrow = rng.Row
Do While rng.Column = i
rng.Characters(InStr(rng, myValue), Len(myValue)).Font.ColorIndex = 3
Set rng = Cells.FindNext(After:=rng)
If oldrngrow = rng.Row Then
Exit Do
End If
Loop
Skip:
Next i
End Sub
https://stackoverflow.com/questions/43416108
复制相似问题