+ Reply to Thread
Results 1 to 2 of 2

Search a word and highlight

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    11-18-2007
    Posts
    111

    Search a word and highlight

    Hi,
    I have this code and would like to modify little bit with your help. Here is what i want to do:
    1)I would like to search each cell in every column in the entire worksheet. The code currently searches only in column A and i would like to expand it in the entire sheet.
    2) I need only to search and highlight if it finds the list of words that i specified like the word "BT" or "BTG" But the problem with this code is that it finds and highlights any word that contain the words "BT" or "BTG". For instance, it finds like this word "BTGL" or "BTA" but i want only to find the words that i specify in my list. I would really appreciate if you can help me with this. Thank you so much.
    Sub FindAndHighlight(SearchData As Variant, SearchRange As Range, HighlightColor As Long)
    
       Dim FirstAddress As String
       Dim SearchCell As Range
       Dim SrcWks As Worksheet
    
         Set SrcWks = Worksheets(SearchRange.Parent.Name)
         
         Set SearchCell = SearchRange.Find(What:=SearchData, After:=SearchRange.Cells(1, 1), _
                            LookIn:=xlValues, LookAt:=xlPart, SearchOrder:=xlByColumns, _
                            SearchDirection:=xlNext, MatchCase:=False)
                            
         If Not SearchCell Is Nothing Then
            FirstAddress = SearchCell.Address
              Do
                  SearchCell.Cells.Interior.ColorIndex = HighlightColor
                  
                Set SearchCell = SearchRange.FindNext(SearchCell)
              Loop While Not SearchCell Is Nothing And SearchCell.Address <> FirstAddress
         End If
              
    End Sub
    
    Sub HighlightRisk()
      
       Dim LastRow As Long
       Dim RiskCol As Variant
       Dim RiskRange As Range
       Dim StartRow As Long
         
        RiskCol = "A"
         StartRow = 1
          With Worksheets("Sheet1")
            LastRow = .Cells(.Rows.Count, RiskCol).End(xlUp).Row
            LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
            Set RiskRange = .Range(.Cells(StartRow, RiskCol), .Cells(LastRow, RiskCol))
          End With
          
          FindAndHighlight "BT", RiskRange, 3      'Red
          FindAndHighlight "BTG", RiskRange, 5    'Blue
          FindAndHighlight "BTI", RiskRange, 4       'Green
          FindAndHighlight "BTP", RiskRange, 1
          FindAndHighlight "NT", RiskRange, 16
          FindAndHighlight "NTG", RiskRange, 2
          FindAndHighlight "NTI", RiskRange, 8
          FindAndHighlight "NTP", RiskRange, 9
          FindAndHighlight "PT", RiskRange, 10
          FindAndHighlight "RT", RiskRange, 11
          FindAndHighlight "SQE", RiskRange, 12
          FindAndHighlight "TR", RiskRange, 13
          FindAndHighlight "TRSYN", RiskRange, 14
          FindAndHighlight "TT", RiskRange, 15
    End Sub

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259
    Hello karinos57,

    You need to change a the search value LookAt from xlPart to xlWhole if you want to match the whole word and not just part of the word. See the code in blue below...
    ub FindAndHighlight(SearchData As Variant, SearchRange As Range, HighlightColor As Long)
    
       Dim FirstAddress As String
       Dim SearchCell As Range
       Dim SrcWks As Worksheet
    
         Set SrcWks = Worksheets(SearchRange.Parent.Name)
         
         Set SearchCell = SearchRange.Find(What:=SearchData, After:=SearchRange.Cells(1, 1), _
                            LookIn:=xlValues, LookAt:=xlWhole, SearchOrder:=xlByColumns, _
                            SearchDirection:=xlNext, MatchCase:=False)
                            
         If Not SearchCell Is Nothing Then
            FirstAddress = SearchCell.Address
              Do
                  SearchCell.Cells.Interior.ColorIndex = HighlightColor
                  
                Set SearchCell = SearchRange.FindNext(SearchCell)
              Loop While Not SearchCell Is Nothing And SearchCell.Address <> FirstAddress
         End If
              
    End Sub
    
    Sub HighlightRisk()
      
       Dim LastRow As Long
       Dim RiskCol As Variant
       Dim RiskRange As Range
       Dim StartRow As Long
         
        RiskCol = "A"
         StartRow = 1
          With Worksheets("Sheet1")
            LastRow = .Cells(.Rows.Count, RiskCol).End(xlUp).Row
            LastRow = IIf(LastRow < StartRow, StartRow, LastRow)
            Set RiskRange = .Range(.Cells(StartRow, RiskCol), .Cells(LastRow, RiskCol))
          End With
          
          FindAndHighlight "BT", RiskRange, 3      'Red
          FindAndHighlight "BTG", RiskRange, 5    'Blue
          FindAndHighlight "BTI", RiskRange, 4       'Green
          FindAndHighlight "BTP", RiskRange, 1
          FindAndHighlight "NT", RiskRange, 16
          FindAndHighlight "NTG", RiskRange, 2
          FindAndHighlight "NTI", RiskRange, 8
          FindAndHighlight "NTP", RiskRange, 9
          FindAndHighlight "PT", RiskRange, 10
          FindAndHighlight "RT", RiskRange, 11
          FindAndHighlight "SQE", RiskRange, 12
          FindAndHighlight "TR", RiskRange, 13
          FindAndHighlight "TRSYN", RiskRange, 14
          FindAndHighlight "TT", RiskRange, 15
    End Sub
    Sincerely,
    Leith Ross

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1