突出显示单元格中长文本中的所有单词
Highlight all words in a long text that is in a Cell
我正在尝试开发一个查找按钮,以红色标记单元格中包含的单词的“全部”。
例如,如果我的单元格中有这段文字。
“Pepper 口袋里有薄荷糖”
应该改成这个。
“Pepper 口袋里有 pepper薄荷
此代码突出显示它找到的第一个单词。
Dim i As Long
Dim oldrngrow As Long
Dim myValue As String
Dim arr() As Variant
arr = Array(TextBox1.Value)
TextBox2.Text = UBound(arr)
For i = 1 To UBound(arr) + 1
myValue = arr(i - 1)
If myValue = vbNullString Then
MsgBox ("Please Enter a Word in Textbox")
End
End If
Set rng = Cells.Find(What:=myValue, After:=Cells(1, i), LookIn:= _
xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, MatchByte:=True, SearchFormat:=False)
If rng Is Nothing Then
GoTo skip
End If
oldrngrow = rng.Row
Do While rng.Column = i
If ComboBox1.Text = "Red" Then
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
有趣的问题。经过一些研究,我整理了以下代码来演示如何突出显示单元格中字符串中单词的每个实例。为了演示,它使用一个输入框来获取所需的 string-to-highlight(您可以更改方法),并假设要搜索的范围只是 A1 – 同样,您可以将其更改为您想要的任何内容。
确保在子项的顶部包含 Option Compare Text
– 否则搜索将区分大小写。让我知道你过得怎么样。
Option Compare Text
Sub StringColor()
Dim myRange As Range, myCell As Range, myString As String, myCount As Integer
Set myRange = Range("A1")
myString = InputBox("Type the word you want to color in A1")
For Each myCell In myRange
For myCount = 1 To Len(myCell) - Len(myString) + 1
If Mid(myCell, myCount, Len(myString)) = myString Then
myCell.Characters(myCount, Len(myString)).Font.Color = vbRed
End If
Next myCount
Next myCell
End Sub
我正在尝试开发一个查找按钮,以红色标记单元格中包含的单词的“全部”。
例如,如果我的单元格中有这段文字。
“Pepper 口袋里有薄荷糖”
应该改成这个。
“Pepper 口袋里有 pepper薄荷
此代码突出显示它找到的第一个单词。
Dim i As Long
Dim oldrngrow As Long
Dim myValue As String
Dim arr() As Variant
arr = Array(TextBox1.Value)
TextBox2.Text = UBound(arr)
For i = 1 To UBound(arr) + 1
myValue = arr(i - 1)
If myValue = vbNullString Then
MsgBox ("Please Enter a Word in Textbox")
End
End If
Set rng = Cells.Find(What:=myValue, After:=Cells(1, i), LookIn:= _
xlValues, LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:= _
xlNext, MatchCase:=False, MatchByte:=True, SearchFormat:=False)
If rng Is Nothing Then
GoTo skip
End If
oldrngrow = rng.Row
Do While rng.Column = i
If ComboBox1.Text = "Red" Then
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
有趣的问题。经过一些研究,我整理了以下代码来演示如何突出显示单元格中字符串中单词的每个实例。为了演示,它使用一个输入框来获取所需的 string-to-highlight(您可以更改方法),并假设要搜索的范围只是 A1 – 同样,您可以将其更改为您想要的任何内容。
确保在子项的顶部包含 Option Compare Text
– 否则搜索将区分大小写。让我知道你过得怎么样。
Option Compare Text
Sub StringColor()
Dim myRange As Range, myCell As Range, myString As String, myCount As Integer
Set myRange = Range("A1")
myString = InputBox("Type the word you want to color in A1")
For Each myCell In myRange
For myCount = 1 To Len(myCell) - Len(myString) + 1
If Mid(myCell, myCount, Len(myString)) = myString Then
myCell.Characters(myCount, Len(myString)).Font.Color = vbRed
End If
Next myCount
Next myCell
End Sub