2015-09-04 41 views
0

我一直在使用下面,但發現它只突出顯示/更改每個單元格的第一個子字符串的字體顏色。我如何確保它也突出顯示後續的內容?如何突出顯示單元格中特定子字符串的所有出現次數?

Sub test4String2color() 
    Dim strTest As String 
    Dim strLen As Integer 
    strTest = Range("F1") 
    strLen = Len(strTest) 
    For Each cell In Range("A1:D100") 
     If InStr(cell, strTest) > 0 Then 
     cell.Characters(InStr(cell, strTest), strLen).Font.Color = vbRed 
    End If 
    Next 
End Sub 

回答

0

你只需要遍歷字符串,直到你無法找到strTest了。使用前一個查找的位置開始連續搜索。

Sub test4String2color() 
    Dim strTest As String, cell As Range 
    Dim strLen As Long, p As Long 

    With ActiveSheet 
     strTest = .Range("F1") 
     strLen = Len(strTest) 
     For Each cell In Intersect(.Range("A1:D100"), .UsedRange) 
      p = InStr(1, cell, strTest) 
      Do While CBool(p) 
       cell.Characters(p, strLen).Font.Color = vbRed 
       p = InStr(p + 1, cell, strTest) 
      Loop 
     Next 
    End With 
End Sub 
相關問題