Closed Thread
Results 1 to 2 of 2

Search and Highlight

Hybrid View

karinos57 Search and Highlight 06-23-2008, 01:41 PM
NBVC Duplicate posts not allowed.... 06-23-2008, 01:49 PM
  1. #1
    Forum Contributor
    Join Date
    11-18-2007
    Posts
    111

    Search 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 Expert NBVC's Avatar
    Join Date
    12-06-2006
    Location
    Mississauga, CANADA
    MS-Off Ver
    2003:2010
    Posts
    34,898
    Duplicate posts not allowed.

    http://www.excelforum.com/showthread.php?t=647763
    Where there is a will there are many ways.

    If you are happy with the results, please add to the contributor's reputation by clicking the reputation icon (star icon) below left corner

    Please also mark the thread as Solved once it is solved. Check the FAQ's to see how.

Closed 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