Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Excel 2010中突出显示同一单元格中给定范围或选定范围内的每个单词实例?_Excel_Vba_Format - Fatal编程技术网

如何在Excel 2010中突出显示同一单元格中给定范围或选定范围内的每个单词实例?

如何在Excel 2010中突出显示同一单元格中给定范围或选定范围内的每个单词实例?,excel,vba,format,Excel,Vba,Format,我希望使用Excel 2010以红色和粗体突出显示我的Excel工作表所选列中单词/短语的每个实例。例如,如果列A1:A10包含“棕色狐狸喜欢另一只棕色狐狸”这句话,我想突出显示该范围内的每个棕色狐狸实例 我发现了一个宏,它只在每个单元格中突出显示brown fox的第一个实例: Sub colorText() Dim cl As Range Dim startPos As Integer Dim totalLen As Integer Dim searchText As String '

我希望使用Excel 2010以红色和粗体突出显示我的Excel工作表所选列中单词/短语的每个实例。例如,如果列A1:A10包含“棕色狐狸喜欢另一只棕色狐狸”这句话,我想突出显示该范围内的每个棕色狐狸实例

我发现了一个宏,它只在每个单元格中突出显示brown fox的第一个实例:

Sub colorText()

Dim cl As Range
Dim startPos As Integer
Dim totalLen As Integer
Dim searchText As String

' specify text to searh.
searchText = "brown fox"

' loop trough all cells in selection/range
For Each cl In Selection

  totalLen = Len(searchText)
  startPos = InStr(cl, searchText)

  If startPos > 0 Then
    With cl.Characters(startPos, totalLen).Font
      .FontStyle = "Bold"
      .ColorIndex = 3
    End With
  End If
Next cl

End Sub
我想编辑这个宏,以便它突出显示brown fox的每个实例,而不仅仅是第一个实例。作为一种尝试,我尝试了以下方法:

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 = "brown fox"

' 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, searchText)
  Loop

Next cl

End Sub
然而,这仍然只格式化了brown fox的第一个实例


任何想法/编辑都将不胜感激。

您的错误在于您的逻辑。您应按以下方式更正代码:

 startPos = InStr(testPos, cl, searchText, vbTextCompare)
而不是这样做:


在第二小节中,你现在看到了吗?:-

您的错误在您的逻辑上。您应按以下方式更正代码:

 startPos = InStr(testPos, cl, searchText, vbTextCompare)
而不是这样做:


在第二小节中,你现在看到了吗?:-

当我在一系列单元格中设置特定单词的格式时,我也遇到了同样的问题。经过几次尝试和大量的互联网搜索,这是一个工作得最好的

Sub FormatWords()
Dim Rng As Range, cl As Range, Red As Integer
Dim oStrg As String
Set Rng = Range(Range("D1"), Range("D" & Rows.Count).End(xlUp))
On Error Resume Next
oStrg = "Apple"
If oStrg = "" Then Exit Sub

For Each cl In Rng
    Red = InStr(1, cl, oStrg, vbTextCompare)

    Do Until Red = 0
        With cl.Characters(Red, Len(oStrg))
        .Font.Color = RGB(230, 25, 55)
        .Font.Bold = True
         End With
         Red = InStr(Red + 1, cl, oStrg, vbTextCompare)
    Loop
Next cl

oStrg = "Mango"
If oStrg = "" Then Exit Sub

For Each cl In Rng
    Orange = InStr(1, cl, oStrg, vbTextCompare)
    Do Until Orange = 0
        With cl.Characters(Orange, Len(oStrg))
        .Font.Color = RGB(250, 200, 0)
        .Font.Bold = True
        End With

        Orange = InStr(Orange + 1, cl, oStrg, vbTextCompare)
    Loop
Next cl

End Sub

当我在一系列单元格中设置特定单词的格式时,我也遇到了同样的问题。经过几次尝试和大量的互联网搜索,这是一个工作得最好的

Sub FormatWords()
Dim Rng As Range, cl As Range, Red As Integer
Dim oStrg As String
Set Rng = Range(Range("D1"), Range("D" & Rows.Count).End(xlUp))
On Error Resume Next
oStrg = "Apple"
If oStrg = "" Then Exit Sub

For Each cl In Rng
    Red = InStr(1, cl, oStrg, vbTextCompare)

    Do Until Red = 0
        With cl.Characters(Red, Len(oStrg))
        .Font.Color = RGB(230, 25, 55)
        .Font.Bold = True
         End With
         Red = InStr(Red + 1, cl, oStrg, vbTextCompare)
    Loop
Next cl

oStrg = "Mango"
If oStrg = "" Then Exit Sub

For Each cl In Rng
    Orange = InStr(1, cl, oStrg, vbTextCompare)
    Do Until Orange = 0
        With cl.Characters(Orange, Len(oStrg))
        .Font.Color = RGB(250, 200, 0)
        .Font.Bold = True
        End With

        Orange = InStr(Orange + 1, cl, oStrg, vbTextCompare)
    Loop
Next cl

End Sub

这适用于grat for Excel 2010。希望这也适用于您的版本。最好在你的问题中包含它,好吗?这对Excel 2010很有用。希望这也适用于您的版本。最好把它包括在你的问题里,好吗?