通过仅匹配整个单词来突出显示单元格(而不是单元格)中的单词?
Highlight words in a cell (not the cell) by matching WHOLE WORDS only?
我正在尝试为 excel 文档编写代码,以实现以下目的:
- 在工作表中搜索一组列出的单词(由名称管理员定义)
- 仅将列出的单词作为完整单词搜索,同时考虑区分大小写、preceded/followed 标点符号等
- 将单元格(而不是单元格本身)中列出的单词格式化为新的字体颜色(理想情况下我希望突出显示它,但我不确定 Excel 是否允许这样做)。
我目前有下面列出的代码,它以黄色突出显示单元格并将列出的单词变为红色 - 但它匹配单词中的出现。我怎样才能让它只匹配整个单词?
Sub ColorCertainWords()
Dim Z As Long, Position As Long, Words As Variant, Cell As Range
Words = Range("LIST") 'LIST defined by name manager as list of words that cannot be used
For Each Cell In Sheets("Sheet1").Range("A1:AA6000") 'Range of cells to be checked
If Len(Cell.Value) Then
For Z = 1 To UBound(Words)
Position = InStr(1, Cell.Value, Words(Z, 1), vbTextCompare)
Do While Position
Cell.Characters(Position, Len(Words(Z, 1))).Font.ColorIndex = 3 'Red
Cell.Interior.ColorIndex = 6 ' Yellow
Position = InStr(Position + 1, Cell.Value, Words(Z, 1), vbTextCompare)
Loop
Next
End If
Next
End Sub
这是您修改后的代码,可以帮助您继续前进。
Sub ColorCertainWords()
Dim Z As Long, Position As Long, Words As Variant
Dim Cell As Range, x As Integer, j As Integer
Dim tempWords As Variant
Words = Range("LIST")
x = 1
For Each Cell In Sheets("Sheet6").Range("A1:A6") 'Range of cells to be checked
If Len(Cell.Value) Then
tempWords = Split(Cell.Value, " ") 'Splitting cell value by space
For i = LBound(tempWords) To UBound(tempWords) 'Looping through splitted values
j = InStr(x, Cell.Value, " ") + 1
For Z = 1 To UBound(Words)
If tempWords(i) = Words(Z, 1) Then 'Checking is words are matching
For k = 1 To Len(tempWords(i))
Cell.Characters(x, Len(tempWords(i))).Font.ColorIndex = 3 'Red
Cell.Interior.ColorIndex = 6 ' Yellow
Next
End If
Next
x = j
Next
x = 1
End If
Next
End Sub
这是我使用的结果格式的测试数据:
让我知道这是否有帮助。
我正在尝试为 excel 文档编写代码,以实现以下目的:
- 在工作表中搜索一组列出的单词(由名称管理员定义)
- 仅将列出的单词作为完整单词搜索,同时考虑区分大小写、preceded/followed 标点符号等
- 将单元格(而不是单元格本身)中列出的单词格式化为新的字体颜色(理想情况下我希望突出显示它,但我不确定 Excel 是否允许这样做)。
我目前有下面列出的代码,它以黄色突出显示单元格并将列出的单词变为红色 - 但它匹配单词中的出现。我怎样才能让它只匹配整个单词?
Sub ColorCertainWords()
Dim Z As Long, Position As Long, Words As Variant, Cell As Range
Words = Range("LIST") 'LIST defined by name manager as list of words that cannot be used
For Each Cell In Sheets("Sheet1").Range("A1:AA6000") 'Range of cells to be checked
If Len(Cell.Value) Then
For Z = 1 To UBound(Words)
Position = InStr(1, Cell.Value, Words(Z, 1), vbTextCompare)
Do While Position
Cell.Characters(Position, Len(Words(Z, 1))).Font.ColorIndex = 3 'Red
Cell.Interior.ColorIndex = 6 ' Yellow
Position = InStr(Position + 1, Cell.Value, Words(Z, 1), vbTextCompare)
Loop
Next
End If
Next
End Sub
这是您修改后的代码,可以帮助您继续前进。
Sub ColorCertainWords()
Dim Z As Long, Position As Long, Words As Variant
Dim Cell As Range, x As Integer, j As Integer
Dim tempWords As Variant
Words = Range("LIST")
x = 1
For Each Cell In Sheets("Sheet6").Range("A1:A6") 'Range of cells to be checked
If Len(Cell.Value) Then
tempWords = Split(Cell.Value, " ") 'Splitting cell value by space
For i = LBound(tempWords) To UBound(tempWords) 'Looping through splitted values
j = InStr(x, Cell.Value, " ") + 1
For Z = 1 To UBound(Words)
If tempWords(i) = Words(Z, 1) Then 'Checking is words are matching
For k = 1 To Len(tempWords(i))
Cell.Characters(x, Len(tempWords(i))).Font.ColorIndex = 3 'Red
Cell.Interior.ColorIndex = 6 ' Yellow
Next
End If
Next
x = j
Next
x = 1
End If
Next
End Sub
这是我使用的结果格式的测试数据:
让我知道这是否有帮助。