简体   繁体   中英

Compare two cell values and highlight only text

I have comma-separated words in column L2 and I have to compare them with text/paragraph in O2, P2, and Q2.

I browsed through questions and got it for 1 column but unable to iterate for all columns.

For example. I want to compare:
L3 only with O3, P3 and Q3
and
L4 only with O4, P4 and Q4
and so on for all values in the active worksheet and highlight the text alone.

sample:

  • L2: cookie, policy
  • O2: By using our site, you acknowledge that you have read and understood our Cookie Policy, Privacy Policy, and our Terms of Service.

Cookie Policy and Policy should be highlighted.

Sub HighlightCells()
Dim UserRange As Range
Dim arySearch As Variant
Dim searchRng As Range
Dim rng As Range
Dim cel As Range
Dim i As Long, ii As Long

Set UserRange = ActiveSheet.Range("O2:Q2")

Set rng = ActiveSheet.Range("L2")

If rng = "" Then Exit Sub
arySearch = Split(rng, ",")

For Each cel In UserRange
    With cel
        For ii = LBound(arySearch) To UBound(arySearch)
            i = InStr(cel.Value, arySearch(ii))
            If i > 0 Then
                .Characters(i, Len(arySearch(ii))).Font.ColorIndex = 3
            End If
        Next ii
    End With
Next cel

Application.ScreenUpdating = True
End Sub 

I've written a version of this which should work for you, if I've understood properly what you are trying to achieve. Sorry for renaming some things, that's just my habit. You can change it however you want.

Sub HighlightCells()
Dim UserRange As Range
Dim SearchRange As Range
Dim SearchArray As Variant
Dim String1 As String
Dim String2 As String
Dim LastRow As Long
Dim RowIndex As Long
Dim ColumnIndex As Long
Dim Index As Long
Dim SearchIndex As Long

Application.ScreenUpdating = False

LastRow = Range("L" & Rows.Count).End(xlUp).Row
Set UserRange = ActiveSheet.Range("O2:Q" & LastRow)
Set SearchRange = ActiveSheet.Range("L2:L" & LastRow)

For RowIndex = 1 To SearchRange.Rows.Count
    SearchArray = Split(SearchRange(RowIndex), ",")
    
    For SearchIndex = LBound(SearchArray) To UBound(SearchArray)
        String2 = Trim(SearchArray(SearchIndex))
        
        If String2 <> "" Then
            For ColumnIndex = 1 To UserRange.Columns.Count
                String1 = UserRange(RowIndex, ColumnIndex)
                
                Index = InStr(1, String1, String2, vbTextCompare)
                
                Do Until Index = 0
                    UserRange(RowIndex, ColumnIndex).Characters(Index, Len(String2)).Font.ColorIndex = 3
                    Index = InStr(Index + 1, String1, String2, vbTextCompare)
                Loop
            Next ColumnIndex
        End If
    Next SearchIndex
Next RowIndex

Application.ScreenUpdating = True
End Sub

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