我在 I 列和 H 列中的值很少,如果這些單詞完全存在于 I 列中,我有一個代碼可以突出顯示 H 列中的特定單詞。
缺點是只有當它們完全一樣并且一起出現時才會突出顯示作品,是否可以在代碼中進行任何更改并突出顯示每個單詞,即使它們不在一起

附上我想要的與我擁有的影像,還附上現有的代碼。
Dim c1 As Range, c2 As Range, md As Variant, i As Long, w1 As String, os As Long
Set c1 = Range("I2")
Set c2 = Range("H2")
md = Range(c1, Cells(Rows.Count, c1.Column).End(xlUp)).Value
For i = 1 To UBound(md)
If md(i, 1) <> "" Then
w1 = c2.Cells(i, 1).Value
os = InStr(1, w1, md(i, 1), vbTextCompare)
While os > 0
c2.Cells(i, 1).Characters(Start:=os, Length:=Len(md(i, 1))).Font.Color = vbBlue
os = InStr(os 1, w1, md(i, 1), vbTextCompare)
Wend
End If
Next i
如果有人解決了我的問題,那將是一個很大的幫助。
uj5u.com熱心網友回復:
對于模式匹配,使用正則運算式。
Option Explicit
Sub markup()
Dim regex As Object, m As Object, ar
Dim pattern As String, s As String
Dim Lastrow As Long, i As Long, k As Long, n As Long, p As Long
' Create regular expression.
Set regex = CreateObject("VBScript.RegExp")
With regex
.IgnoreCase = True
.Global = True
End With
'update sheet
With ActiveSheet
Lastrow = .Cells(.Rows.Count, "I").End(xlUp).Row
For i = 2 To Lastrow
pattern = Replace(.Cells(i, "I"), ",", "|")
If Len(pattern) > 0 Then
regex.pattern = pattern
s = .Cells(i, "H")
If regex.test(s) Then
' markup matches
Set m = regex.Execute(s)
For k = 0 To m.Count - 1
p = m(k).firstindex 1
n = Len(m(k))
With .Cells(i, "H").Characters(Start:=p, Length:=n)
.Font.Color = vbBlue
.Font.Bold = True
End With
Next
End If
End If
Next
End With
End Sub
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/419974.html
標籤:
下一篇:在for回圈中動態設定物件名稱
