Oh yes, I took a lot from both of your codes and came up with a solution that can scan for different words, and highlight with multiple words.
Option ExplicitSub ExampleMacro()
Dim oTbl As Table
Dim oRng As Range
Set oTbl = ActiveDocument.Tables(3)
Dim lngIndex As Long, lngIndex2 As Long, lngIndex3 As Long, lngIndex4 As Long, lngIndex5 As Long, lngCount As Long, lngCount2 As Long, lngCount3 As Long, lngCount4 As Long, lngCount5 As Long
Dim arrTerms() As String
Dim arrTerms2() As String
Dim arrTerms3() As String
Dim arrTerms4() As String
Dim arrTerms5() As String
arrTerms = Split("Pancakes (),Pancakes,Information,Started,Concluded", ",")
arrTerms2 = Split("Waffles (),Waffles", ",")
arrTerms3 = Split("Oranges (),Oranges", ",")
arrTerms4 = Split("Blueberries (),Blueberries", ",")
arrTerms5 = Split("Pineapples (),Pineapples", ",")
For lngIndex = 0 To UBound(arrTerms)
Set oRng = oTbl.Range
lngCount = 1
With oRng.Find
Do While .Execute(FindText:=arrTerms(lngIndex))
If oRng.InRange(oTbl.Range) Then
oRng.Rows(1).Range.Shading.BackgroundPatternColor = wdColorYellow
oRng.Text = Replace(oRng.Text, "()", "(" & lngCount & ")")
lngCount = lngCount + 1
End If
oRng.Collapse 0
Loop
End With
Next
For lngIndex2 = 0 To UBound(arrTerms2)
Set oRng = oTbl.Range
lngCount2 = 1
With oRng.Find
Do While .Execute(FindText:=arrTerms2(lngIndex2))
If oRng.InRange(oTbl.Range) Then
oRng.Rows(1).Range.Shading.Texture = wdTextureNone
oRng.Rows(1).Range.Shading.ForegroundPatternColor = wdColorAutomatic
oRng.Rows(1).Range.Shading.BackgroundPatternColor = 12611584
oRng.Text = Replace(oRng.Text, "()", "(" & lngCount2 & ")")
lngCount2 = lngCount2 + 1
End If
oRng.Collapse 0
Loop
End With
Next
For lngIndex3 = 0 To UBound(arrTerms3)
Set oRng = oTbl.Range
lngCount3 = 1
With oRng.Find
Do While .Execute(FindText:=arrTerms3(lngIndex3))
If oRng.InRange(oTbl.Range) Then
oRng.Rows(1).Range.Shading.Texture = wdTextureNone
oRng.Rows(1).Range.Shading.ForegroundPatternColor = wdColorAutomatic
oRng.Rows(1).Range.Shading.BackgroundPatternColor = 49407
oRng.Text = Replace(oRng.Text, "()", "(" & lngCount3 & ")")
lngCount3 = lngCount3 + 1
End If
oRng.Collapse 0
Loop
End With
Next
For lngIndex4 = 0 To UBound(arrTerms4)
Set oRng = oTbl.Range
lngCount4 = 1
With oRng.Find
Do While .Execute(FindText:=arrTerms4(lngIndex4))
If oRng.InRange(oTbl.Range) Then
oRng.Rows(1).Range.Shading.Texture = wdTextureNone
oRng.Rows(1).Range.Shading.ForegroundPatternColor = wdColorAutomatic
oRng.Rows(1).Range.Shading.BackgroundPatternColor = 15773696
oRng.Text = Replace(oRng.Text, "()", "(" & lngCount4 & ")")
lngCount4 = lngCount4 + 1
End If
oRng.Collapse 0
Loop
End With
Next
For lngIndex5 = 0 To UBound(arrTerms5)
Set oRng = oTbl.Range
lngCount5 = 1
With oRng.Find
Do While .Execute(FindText:=arrTerms5(lngIndex5))
If oRng.InRange(oTbl.Range) Then
oRng.Rows(1).Range.Shading.Texture = wdTextureNone
oRng.Rows(1).Range.Shading.ForegroundPatternColor = wdColorAutomatic
oRng.Rows(1).Range.Shading.BackgroundPatternColor = 5296274
oRng.Text = Replace(oRng.Text, "()", "(" & lngCount5 & ")")
lngCount5 = lngCount5 + 1
End If
oRng.Collapse 0
Loop
End With
Next
lbl_Exit:
Set oTbl = Nothing
Set oRng = Nothing
Exit Sub
End Sub
The next things I would like to do with this code is to implement a counter, so that the count doesn't start over from 0 everytime the code is run.
Let's say I have this after running the code:
Pancakes (1)
Pancakes (2)
If I add another one like so:
Pancakes (1)
Pancakes (2)
Pancakes ()
And run the code again, the count starts at one again, which gives me this:
Pancakes (1)
Pancakes (2)
Pancakes (1)
I have a table at the bottom of the word document, so I will try to do is modify it so that it shows
Col 1 Col2
Pancakes Current count
That way, I can set the starting counter to be that table's number, and things can be update dynamically
Although actually implementing will probably take a bit longer hehe.