Excel VBA セル内の特定の文字列に色を付けるマクロです。
(2021/02/16) 複数文字列へ対応。
Private Sub CommandButton1_Click()
' 探索対象範囲(列、行数)
Const col As Integer = 1
Const rows As Integer = 50
' 探索文字列
Dim target(0 To 2) As String
target(0) = "apple"
target(1) = "banana"
target(2) = "cherry"
Dim index As Integer
Dim index2 As Integer
Dim tgt As String
Dim curCel As String
Dim res As Integer
For index = 1 To rows
Cells(index, col).Select
curCel = ActiveCell.Value
If curCel <> "" Then
For index2 = 0 To UBound(target)
tgt = target(index2)
res = InStr(1, curCel, tgt, vbTextCompare)
While 0 < res
With ActiveCell.Characters(Start:=res, Length:=Len(tgt)).Font
.Color = vbRed
End With
res = InStr(res + 1, curCel, tgt, vbTextCompare)
Wend
Next
End If
Next
Range("A1").Select
End Sub