VBA code for Highlight Specific Text Within A Cell Based On Other Text

Enna

New Member
Joined
Oct 5, 2021
Messages
41
Office Version
  1. 2010
Platform
  1. Windows
I would like to format text from another text.
For example if column "Race" =aa then it should highlight "aa" from the "Text" column PARAAORTIC, see excel excel file.
The file is large so how can I do using VBA code? Thank you.
1634050566567.png
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Open a copy of your workbook. Press Alt-F11 to open the VBA editor. Press Alt-IM to Insert a Module. Copy the following code into the window that opens:

VBA Code:
Sub HighlightText()
Dim c1 As Range, c2 As Range, md As Variant, i As Long, w1 As String, os As Long

    Set c1 = Range("A2")
    Set c2 = Range("B2")
    
    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 = vbRed
                os = InStr(os + 1, w1, md(i, 1), vbTextCompare)
            Wend
        End If
    Next i
            
End Sub

On the first 2 lines (that say Set), change the cell reference to the first cell with data in it for each column. Press Alt-Q to close the VBA editor. On the sheet with the data, press Alt-F8 to open the macro selector. Select HighlightText and press Run.

Hope this helps!
 
Upvote 0
Open a copy of your workbook. Press Alt-F11 to open the VBA editor. Press Alt-IM to Insert a Module. Copy the following code into the window that opens:

VBA Code:
Sub HighlightText()
Dim c1 As Range, c2 As Range, md As Variant, i As Long, w1 As String, os As Long

    Set c1 = Range("A2")
    Set c2 = Range("B2")
   
    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 = vbRed
                os = InStr(os + 1, w1, md(i, 1), vbTextCompare)
            Wend
        End If
    Next i
           
End Sub

On the first 2 lines (that say Set), change the cell reference to the first cell with data in it for each column. Press Alt-Q to close the VBA editor. On the sheet with the data, press Alt-F8 to open the macro selector. Select HighlightText and press Run.

Hope this helps!
Sorry, it does not do anything
 
Upvote 0
I'm not sure what to tell you. Here's the results of my test:

1634074473601.png


Are you running it while the sheet with the data in it has the focus? Are you using columns A and B?
 
Upvote 0
Whew! You had me worried! Glad it works for you, and thanks for letting me know! ?
 
Upvote 0

Forum statistics

Threads
1,223,905
Messages
6,175,297
Members
452,633
Latest member
DougMo

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top