次のようなテキストを含む行の列がたくさんあります。
dog,cat,mouse
bat,dog,fly
fish,beaver,horse
特定の単語を含む行を検索して強調表示しようとしています:
Public Sub MarkDuplicates()
Dim iWarnColor As Integer
Dim rng As Range
Dim rngCell As Variant
Dim LR As Long
Dim vVal
Dim tRow
LR = Cells(Rows.Count, "B").End(xlUp).Row
Set rng = Range("B1:B" & LR)
iWarnColor = xlThemeColorAccent2
For Each rngCell In rng.Cells
tRow = rngCell.Row
If InStr(rngCell.Value, "dog") = 1 Then
rngCell.Interior.ColorIndex = iWarnColor
Else
rngCell.Interior.Pattern = xlNone
End If
Next
サブ終了
これは、'dog' という単語がコンマ文字列の最初の単語である限り問題なく機能します。したがって、最初の行が強調表示されますが、2 行目ではなく、'dog' という単語が 'bat' の後に現れるためです。最初にカンマを削除する必要がありますか、それとももっと良い方法がありますか?