For the data provided.
Sub test()
Dim r As Range, a, e, w, i As Long, s
Dim mtch As Object, m As Object, dic As Object
Set dic = CreateObject("Scripting.Dictionary")
Set r = Range("e4", Range("e" & Rows.Count).End(xlUp))
a = r.Value: r.Font.ColorIndex = xlAutomatic
With CreateObject("VBScript.RegExp")
.Global = True
.MultiLine = True
For i = 1 To UBound(a, 1)
.Pattern = "^[A-Z]+(?=\d+\b)"
If .test(a(i, 1)) Then
s = .Execute(a(i, 1))(0)
.Pattern = "\S+"
Set mtch = .Execute(a(i, 1))
.Pattern = s & "\d+"
For Each m In mtch
If Not .test(m) Then
r(i).Characters(m.firstindex + 1, m.Length).Font.Color = vbRed
Else
If Not dic.exists(m.Value) Then
ReDim w(1 To 2, 1 To 1)
Else
w = dic(m.Value)
ReDim Preserve w(1 To 2, 1 To UBound(w, 2) + 1)
End If
Set w(1, UBound(w, 2)) = r(i)
w(2, UBound(w, 2)) = Array(m.firstindex + 1, m.Length)
dic(m.Value) = w
End If
Next
End If
Next
End With
For Each e In dic
If UBound(dic(e), 2) > 1 Then
w = dic(e)
For i = 1 To UBound(w, 2) - 1
If w(1, i).Address = w(1, i + 1).Address Then
w(1, i + 1).Characters(w(2, i + 1)(0), w(2, i + 1)(1)).Font.Color = vbRed
Else
w(1, i).Characters(w(2, i)(0), w(2, i)(1)).Font.Color = vbRed
End If
Next
End If
Next
End Sub
Bookmarks