代码以突出显示Excel中特定列中的特定单词

我正在寻找在特定列中search特定关键字并将其突出显示为黄色的Excel代码。 并能够做到这一点的多个列,每个与它自己的不同的关键字。

例:

  • search关键字“河”的列A
  • search关键字“ocean”的B列
  • search关键字“海”的C列

每次只有特定列中突出显示唯一关键字,即使它也可能出现在其他列中。

该代码将包含从“列A”到“列CV”的100列,并允许为每列插入唯一关键字。

这可能吗?

通过search论坛,我发现在Excel中突出显示特定单词的代码,但是没有一个将search范围缩小到一列,并从其他列中排除该关键字。

这个代码,find一个字,并把它的颜色变成红色,有一个类似的核心思想:

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。

谢谢。

编辑:您可以突出显示单元格或更改单元格中特定文本的字体颜色。 Excel没有选项可以突出显示单元格中特定文本的背景。

既然你只想看到被search的string变成了彩色,我使用Font.ColorIndex属性和红色来代替黄色,以方便查看。

我还宣布了一个数组,以便您可以input预定义的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