+ Reply to Thread
Results 1 to 2 of 2

Highlight cell with many words, with duplicates from row with single words.

Hybrid View

  1. #1
    Registered User
    Join Date
    02-15-2010
    Location
    earth
    MS-Off Ver
    Excel 2007
    Posts
    1

    Highlight cell with many words, with duplicates from row with single words.

    Would like a VBA script / macro that will compare a column of single words per row, to cell with many words, and highlight only the text of duplicates in the part of a cell with many words.

  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

    Re: Highlight cell with many words, with duplicates from row with single words.

    Hello meows,

    This macro checks the contents of each in cell in column "A" that has entry against the words in a comma separated list in cell "B1". You can change these to match the locations you are using. They are marked in red. The words in the list are highlighted by bolding the font and changing it to red. It is not possible to highlight the background of each word individually.
    Sub HighlightWords()
    
      Dim DSO As Object
      Dim I As Long
      Dim Key As String
      Dim R As Long
      Dim Rng As Range
      Dim RngEnd As Range
      Dim Wks As Worksheet
      Dim WordArray As Variant
      Dim WordCell As Range
      
        Set Wks = ActiveSheet
        Set WordCell = Wks.Range("B1")
        
        Set Rng = Wks.Range("A1")
        Set RngEnd = Wks.Cells(Rows.Count, Rng.Column).End(xlUp)
        Set Rng = IIf(RngEnd.Row < Rng.Row, Rng, Wks.Range(Rng, RngEnd))
        
          Set DSO = CreateObject("Scripting.Dictionary")
          DSO.CompareMode = vbTextCompare
          
          WordArray = Split(WordCell, ",")
          
         'Load the word list into the associative array
          For I = 0 To UBound(WordArray)
            If Not DSO.Exists(WordArray(I)) Then
               DSO.Add WordArray(I), 0
            End If
          Next I
          
         'Find words that match the word list
          For R = 1 To Rng.Rows.Count
            Key = Trim(Rng(R))
            If DSO.Exists(Key) Then
              If DSO(Key) = 0 Then
                 I = InStr(1, WordCell, Key, vbTextCompare)
                'Change the text to bold and the color to red
                 With WordCell.Characters(I, Len(Key)).Font
                   .Bold = True
                   .ColorIndex = 3  'Red
                 End With
              End If
             'Increment number of matches
              DSO(Key) = DSO(Key) + 1
            End If
          Next R
         
       'Release object and memory
        Set DSO = Nothing
        
    End Sub
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ 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