Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to highlight every instance of a word in the same cell for a given range or selection in Excel 2010?
    primarykey
    data
    text
    <p>I would like to highlight in red and bold every instance of a word/phrase in the selected column of my Excel sheet (using Excel 2010). For example, if columns A1:A10 contain the sentence, "The brown fox likes the other brown fox," I would like to highlight every instance of "brown fox" in this range.</p> <p>I found a macro <a href="http://www.mrexcel.com/forum/excel-questions/26200-cells-there-code-bold-color-word-s-cell-2.html" rel="nofollow">here</a> which highlights only the first instance of "brown fox" in every cell:</p> <pre><code>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 &gt; 0 Then With cl.Characters(startPos, totalLen).Font .FontStyle = "Bold" .ColorIndex = 3 End With End If Next cl End Sub </code></pre> <p>I'd like to edit this macro so that it highlights every instance of "brown fox," not just the first. As an attempt, I tried the following:</p> <pre><code>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 &gt; 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 </code></pre> <p>However, this still only formats the first instance of "brown fox."</p> <p>Any thoughts/edits would be much appreciated.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload