VBA to search & highlight text in string

ajdifonzo

New Member
Joined
Jul 31, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi,

I'm trying to search for and highlight specific text in a string that could be there more than once. The list of text that I want to search for is in a column on another tab.

in the attached files the Data tab has the area I want to search, column "E", the Search Values tab has the list of values I want to search for.
 

Attachments

  • search excel 2.png
    search excel 2.png
    28.1 KB · Views: 25
  • search excel.png
    search excel.png
    77.8 KB · Views: 24

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Add this code to a standard module (e.g., Module1) in your file and Run Macro.
VBA Code:
Option Explicit

Public Sub HighlightText()

   Dim DataRow As Long
   Dim TextSheet As Worksheet
  
   Set TextSheet = Worksheets("Data")
  
   DataRow = 1
   Do Until TextSheet.Cells(DataRow, "E") = ""
  
      HighlightTextCell TextSheet.Cells(DataRow, "E")
     
      DataRow = DataRow + 1
     
   Loop

   MsgBox "Highlighting complete."
  
End Sub

Public Sub HighlightTextCell(Cell As Range)

   Dim KeySheet As Worksheet
   Dim KeyRow As Long
   Dim StartPos As Long
   Dim Key As String
  
   Set KeySheet = Worksheets("Search Values")
  
   KeyRow = 1
   Do Until KeySheet.Cells(KeyRow, "A") = ""
  
      Key = KeySheet.Cells(KeyRow, "A")
     
      StartPos = 1
      Do Until StartPos = 0

         StartPos = InStr(StartPos, Cell.Value, Key)
         If StartPos > 0 Then
            Cell.Characters(StartPos, Len(Key)).Font.Color = RGB(255, 0, 0)
            Cell.Characters(StartPos, Len(Key)).Font.Bold = True
            StartPos = StartPos + Len(Key)
         End If
      Loop
  
      KeyRow = KeyRow + 1
  
   Loop

End Sub
 
Upvote 0
Solution
Add this code to a standard module (e.g., Module1) in your file and Run Macro.
VBA Code:
Option Explicit

Public Sub HighlightText()

   Dim DataRow As Long
   Dim TextSheet As Worksheet
 
   Set TextSheet = Worksheets("Data")
 
   DataRow = 1
   Do Until TextSheet.Cells(DataRow, "E") = ""
 
      HighlightTextCell TextSheet.Cells(DataRow, "E")
    
      DataRow = DataRow + 1
    
   Loop

   MsgBox "Highlighting complete."
 
End Sub

Public Sub HighlightTextCell(Cell As Range)

   Dim KeySheet As Worksheet
   Dim KeyRow As Long
   Dim StartPos As Long
   Dim Key As String
 
   Set KeySheet = Worksheets("Search Values")
 
   KeyRow = 1
   Do Until KeySheet.Cells(KeyRow, "A") = ""
 
      Key = KeySheet.Cells(KeyRow, "A")
    
      StartPos = 1
      Do Until StartPos = 0

         StartPos = InStr(StartPos, Cell.Value, Key)
         If StartPos > 0 Then
            Cell.Characters(StartPos, Len(Key)).Font.Color = RGB(255, 0, 0)
            Cell.Characters(StartPos, Len(Key)).Font.Bold = True
            StartPos = StartPos + Len(Key)
         End If
      Loop
 
      KeyRow = KeyRow + 1
 
   Loop

End Sub
Thank you very much. this worked like a charm and saved me hours.
 
Upvote 0
Glad to help! I tested it but am always happy to hear it worked on your side too.
 
Upvote 0
Add this code to a standard module (e.g., Module1) in your file and Run Macro.
VBA Code:
Option Explicit

Public Sub HighlightText()

   Dim DataRow As Long
   Dim TextSheet As Worksheet
 
   Set TextSheet = Worksheets("Data")
 
   DataRow = 1
   Do Until TextSheet.Cells(DataRow, "E") = ""
 
      HighlightTextCell TextSheet.Cells(DataRow, "E")
    
      DataRow = DataRow + 1
    
   Loop

   MsgBox "Highlighting complete."
 
End Sub

Public Sub HighlightTextCell(Cell As Range)

   Dim KeySheet As Worksheet
   Dim KeyRow As Long
   Dim StartPos As Long
   Dim Key As String
 
   Set KeySheet = Worksheets("Search Values")
 
   KeyRow = 1
   Do Until KeySheet.Cells(KeyRow, "A") = ""
 
      Key = KeySheet.Cells(KeyRow, "A")
    
      StartPos = 1
      Do Until StartPos = 0

         StartPos = InStr(StartPos, Cell.Value, Key)
         If StartPos > 0 Then
            Cell.Characters(StartPos, Len(Key)).Font.Color = RGB(255, 0, 0)
            Cell.Characters(StartPos, Len(Key)).Font.Bold = True
            StartPos = StartPos + Len(Key)
         End If
      Loop
 
      KeyRow = KeyRow + 1
 
   Loop

End Sub
Hi,

6StringJazzer,​

I hope you are doing well.
For me the solution that you gave via that code will be useful as well. There is only one adjustment needed. I will truly appreciate if you advise me. Instead of highlighting the founded words/text, i need that text to be copied into the next cell & searching should be : cell by cell for the whole column . Please check at the screenshots. You could see that there is words from column "C" which were allocated in column "D", that is perfect. Unfortunately it is working only for one cell - "C2", not for all cell's from column C - C3,C4,C5 etc. Thanks a lot in advance. Regards!
 

Attachments

  • Screenshot 2024-08-28 160330.png
    Screenshot 2024-08-28 160330.png
    55.5 KB · Views: 6
  • Screenshot 2024-08-28 160242.png
    Screenshot 2024-08-28 160242.png
    51 KB · Views: 6
Upvote 0
There is only one adjustment needed
Hi Sia,

This is not an adjustment, this is a whole new problem. I don't have time to address it at the moment. You will get more attention to your question if you post it as a new thread instead of piggybacking on this one.
 
Upvote 0
This is not an adjustment, this is a whole new problem. I don't have time to address it at the moment. You will get more attention to your question if you post it as a new thread instead of piggybacking on this one.
Thank you!
 
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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