简体   繁体   中英

Vba: Highlight Cell based on Partial Match

I have a good solution to highlighting cells based on an exact condition. I'm trying to highlight based on partial matches. For Example: A1= Dodge. A2 = Durango, A3 = Dart.

How can I do a partial match of lets say "dodg" and "duran" and "dar" then don't highlight the cell but every other cell. Basically if the partial match is missing, then highlight the cell in the entire column.

The following highlights "Durango" if there is a match:

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

you could use

If Highlight Like "Durango" then...

Like will search the cell for that word so it doesn't have to be an exact match to the whole cell, just contain that word or partial word using the "*". So in your case set up a variable for match then use


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM