How to change partial font color in a cell which is found by another cell?

Rajib

New Member
Joined
Mar 11, 2022
Messages
14
Office Version
  1. 2019
Platform
  1. Windows
  2. Web
I want to change the partial font color (red) found in B1 match by column A.
 

Attachments

  • Screenshot_1.jpg
    Screenshot_1.jpg
    38.2 KB · Views: 12

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Use this forum tool XL2BB to share your before state worksheet and accordingly your expected result worksheet …​
 
Upvote 0
Hi & welcome to MrExcel.
Is this for Excel or Google Sheets?
 
Upvote 0
Google Sheets, but if this process is much easier in excel then please suggest the process. Thank you #Fluff
 
Upvote 0
In Excel, it could be done like this.

VBA Code:
Sub HighlighText()
  Dim RX As Object, M As Object
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.Pattern = Join(Application.Transpose(Range("A1", Range("A" & Rows.Count).End(xlUp))), "|")
  With Range("B1")
    For Each M In RX.Execute(.Value)
      .Characters(M.FirstIndex + 1, Len(M)).Font.Color = vbRed
    Next M
  End With
End Sub

1647066869295.png
 
Upvote 0
Awesome. Thank you for your solution.
I faced a couple of problems here.
  1. It is case sensitive, but I need it for both cases (Upper & Lower)
  2. If some word index first then that word is not found with the next word. It occurs when combining 3 words where the middle word is the same. In this case, the first 2 words combine and the second and third word is not combined.
Would you please solve it?
 

Attachments

  • Screenshot_1.jpg
    Screenshot_1.jpg
    92.4 KB · Views: 6
Upvote 0
It is case sensitive, but I need it for both cases (Upper & Lower)
That part is easy. Just add this line
Rich (BB code):
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.IgnoreCase = True

If some word index first then that word is not found with the next word. It occurs when combining 3 words where the middle word is the same. In this case, the first 2 words combine and the second and third word is not combined.
If you are saying that "figurines" should also be coloured then that will require a much less efficient code. That would not be a problem if there is only one cell to process but could add significant time if there are in fact multiple cells in column B to process. Anyway, try this version.

VBA Code:
Sub HighlighText_v2()
  Dim RX As Object, M As Object
  Dim a As Variant, itm As Variant
  Dim i As Long
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.IgnoreCase = True
  a = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value
  With Range("B1")
    For i = 1 To UBound(a)
      RX.Pattern = a(i, 1)
      For Each M In RX.Execute(.Value)
        .Characters(M.FirstIndex + 1, Len(M)).Font.Color = vbRed
      Next M
    Next i
  End With
End Sub
 
Upvote 0
Perfect, thanks much. It's worked.
Is this any other process to execute this in multiple cells in column B or multiple columns like B, C, D....H? I need it for 8 cells. it can be different columns or cells of the rows.
 
Upvote 0
process to execute this in multiple cells in column B or multiple columns like B, C, D....H? I need it for 8 cells
What about something like this?

VBA Code:
Sub HighlighText_v3()
  Dim RX As Object, M As Object
  Dim a As Variant, itm As Variant
  Dim i As Long
  Dim c As Range
  
  Set RX = CreateObject("VBScript.RegExp")
  RX.Global = True
  RX.IgnoreCase = True
  a = Range("A1", Range("A" & Rows.Count).End(xlUp)).Value
  For Each c In Range("B1:C4")
    With c
      For i = 1 To UBound(a)
        RX.Pattern = a(i, 1)
        For Each M In RX.Execute(.Value)
          .Characters(M.FirstIndex + 1, Len(M)).Font.Color = vbRed
        Next M
      Next i
    End With
  Next c
End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,326
Messages
6,124,268
Members
449,149
Latest member
mwdbActuary

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