Vba: 根据部分匹配高亮单元格

Vba: Highlight Cell based on Partial Match

我有一个很好的解决方案,可以根据确切条件突出显示单元格。我试图根据部分匹配来突出显示。例如:A1= 道奇。 A2 = 杜兰戈,A3 = 飞镖。

我如何做部分匹配让我们说 "dodg" 和 "duran" 和 "dar" 然后不突出显示单元格而是每个其他单元格。基本上,如果缺少部分匹配项,则突出显示整个列中的单元格。

以下突出显示 "Durango" 如果有匹配项:

Sub Highlight()

Dim Highlight As Range
Set ws = ActiveSheet
Set w = ws.Rows(1).Find("Dodge", lookat:=xlWhole)
 If Not w Is Nothing Then
    For Each Highlight In ws.Range(w, ws.Cells(Rows.Count, w.Column).End(xlUp)).Cells
            If Highlight = "Durango" Then
                Highlight.Interior.Color = 65535
            End If
        Next Highlight
 End If

End Sub

你可以使用

If Highlight Like "Durango" then...

喜欢将在单元格中搜索该词,因此它不必与整个单元格完全匹配,只需使用“*”包含该词或部分词。因此,在您的情况下,为匹配设置一个变量,然后使用


keyword = Cells(whatever cell your search word is entered into)
keyword = "*" & keyword & "*"
If Highlight Like keyword then
    'code to highlight whatever cells or _
    ' Set a Range and highlight the whole range.
end if