+ Reply to Thread
Results 1 to 12 of 12

Find partial match between two columns and highlight cells that match

Hybrid View

  1. #1
    Registered User
    Join Date
    02-02-2012
    Location
    Canada
    MS-Off Ver
    Excel 2013
    Posts
    42

    Find partial match between two columns and highlight cells that match

    So I have searched for a solution through several forums including this one and I am unable to find a proper one yet. Here's what I need to do.

    I need to search all of the contents of Sheet1 ColumnJ and see if there is a partial match with any cell of Sheet2 Column A. I want to have every cell that is partially matched to be shaded in red and every cell with no match to be shaded blue. Sounds simple and I have tried different methods to no avail.

    Heres what the cells in Sheet1 ColumnJ might look like:
    001127-1-1 / 0 / 110(S)
    001028-5-1 / 0 / 10(S)
    001116-1-1 / 0 / 130(S)
    001095-1-1 / 0 / 110(S)
    001145-1-1 / 0 / 110(S)
    001105-1-1 / 0 / 120(S)

    And Sheet2 Column A Might Be:
    1148
    1145
    1116
    1140
    1105
    1135
    1127
    1132
    1129

    So if run properly, the following cells would be shaded Red
    001127-1-1 / 0 / 110(S)
    001116-1-1 / 0 / 130(S)
    001105-1-1 / 0 / 120(S)

    and the rest would be blue.

  2. #2
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Find partial match between two columns and highlight cells that match

    Why wouldn't the second to bottom line be red (1145)?
    Pauley
    --------
    If I helped with your issue, I'd appreciate a rep bump (hit the '*' icon to the bottom left of this post).

  3. #3
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Find partial match between two columns and highlight cells that match

    Agreed with Pauleyb, i think 1145 should be red too, based on your description. If so, try this code:

    Sub TomToms()
    
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    
    Set CheckRange = ws2.Range("A1:A" & ws2.Cells(Rows.Count, "A").End(xlUp).Row)
    Set SearchRange = ws1.Range("J1:J" & ws1.Cells(Rows.Count, "J").End(xlUp).Row)
    
    For Each Cell In CheckRange
    SearchItem = "*" & Cell.Value & "*"
    Set c = SearchRange.Find(SearchItem)
        If Not c Is Nothing Then c.Interior.Color = vbRed
    Next
    
    For Each Cell In SearchRange
    If Cell.Interior.Color <> vbRed Then
    Cell.Interior.Color = vbBlue
    End If
    Next
    
    End Sub

  4. #4
    Registered User
    Join Date
    02-02-2012
    Location
    Canada
    MS-Off Ver
    Excel 2013
    Posts
    42

    Re: Find partial match between two columns and highlight cells that match

    Yor right, i just missed that one. (Can you see why I need a program lol) This works perfectly! Thankyou so much.

    Another question though, Can this be modified to copy the cell format color from, Column A in Sheet2 to ColumnJ in Sheet1?

    So if there's a match the matched cell will copy the format of the cell it matched with?

  5. #5
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Find partial match between two columns and highlight cells that match

    Sure, if you just mean the interior color of the cell:

    Sub TomToms()
    
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    
    Set CheckRange = ws2.Range("A1:A" & ws2.Cells(Rows.Count, "A").End(xlUp).Row)
    Set SearchRange = ws1.Range("J1:J" & ws1.Cells(Rows.Count, "J").End(xlUp).Row)
    
    For Each Cell In CheckRange
    SearchItem = "*" & Cell.Value & "*"
    Set c = SearchRange.Find(SearchItem)
        If Not c Is Nothing Then 
        c.Interior.Color = Cell.Interior.Color
    Next
    
    'For Each Cell In SearchRange
    'If Cell.Interior.Color <> vbRed Then
    'Cell.Interior.Color = vbBlue
    'End If
    'Next
    
    End Sub
    If you mean copying other formatting, or all the formattings, we can do that, too.

  6. #6
    Forum Expert
    Join Date
    06-26-2010
    Location
    Austin, TX
    MS-Off Ver
    Excel 2010
    Posts
    1,673

    Re: Find partial match between two columns and highlight cells that match

    Glad you got a solution. I used conditional formatting, thus avoiding a macro. Color all of your cells in column J blue. Then use conditional formatting by highlighting the cells in column J, then Conditional Formatting, then 'Use a Formula...', then paste in this formula:
    =NOT(ISERROR(MATCH(--MID(J1,3,4),Sheet2!$A$1:$A$9,0)))
    Set it to format to red, and you should be done. A similar technique can be used for your other sheet.

  7. #7
    Registered User
    Join Date
    02-02-2012
    Location
    Canada
    MS-Off Ver
    Excel 2013
    Posts
    42

    Re: Find partial match between two columns and highlight cells that match

    Thank-you very much for this, it works wonderfully I really appreciate it. I do have another question for it but its slightly a different kind so I will make a new post because its a different type of thing I want to do.

    Thanks again!

  8. #8
    Registered User
    Join Date
    02-02-2012
    Location
    Canada
    MS-Off Ver
    Excel 2013
    Posts
    42

    Re: Find partial match between two columns and highlight cells that match

    Shoot, there's a problem, Seems like this only works for the first match so if 1117 shows up say 4 times only the first time it shows up ill it be highlighted :/

  9. #9
    Forum Expert
    Join Date
    08-28-2014
    Location
    Texas, USA
    MS-Off Ver
    2016
    Posts
    1,796

    Re: Find partial match between two columns and highlight cells that match

    Sub TomToms()
    
    Set ws1 = Worksheets("Sheet1")
    Set ws2 = Worksheets("Sheet2")
    
    Set CheckRange = ws2.Range("A1:A" & ws2.Cells(Rows.Count, "A").End(xlUp).Row)
    Set SearchRange = ws1.Range("J1:J" & ws1.Cells(Rows.Count, "J").End(xlUp).Row)
    
    For Each Cell In CheckRange
    SearchItem = "*" & Cell.Value & "*"
    Set c = SearchRange.Find(SearchItem)
        If Not c Is Nothing Then
        FirstAdd = c.Address
        Do
            c.Interior.Color = Cell.Interior.Color
            Set c = SearchRange.FindNext(c)
        Loop While Not c Is Nothing And c.Address <> FirstAdd
        End If
        
    Next
    End Sub

  10. #10
    Registered User
    Join Date
    07-18-2019
    Location
    Reading Pa.
    MS-Off Ver
    365
    Posts
    2

    Re: Find partial match between two columns and highlight cells that match

    Hi,

    Could you define your variables please? I keep getting Variable not defined errors. Thanks.

  11. #11
    Registered User
    Join Date
    07-18-2019
    Location
    Reading Pa.
    MS-Off Ver
    365
    Posts
    2

    Re: Find partial match between two columns and highlight cells that match

    Hi walruseggman,

    Could you define your variables please? I keep getting variable not defined errors. Thanks.

  12. #12
    Registered User
    Join Date
    02-02-2012
    Location
    Canada
    MS-Off Ver
    Excel 2013
    Posts
    42

    Re: Find partial match between two columns and highlight cells that match

    Awesome, works like a charm. Thanks again

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Highlight cells if data in 2 columns match
    By damobilebrood in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 05-28-2014, 09:19 AM
  2. Find Partial Text Match between Two Columns of data
    By samybelen in forum Excel General
    Replies: 1
    Last Post: 10-23-2013, 01:45 PM
  3. Find and highlight cells in workbook that is NOT an exact match to the cell content
    By AndyHowcroft in forum Excel Programming / VBA / Macros
    Replies: 12
    Last Post: 08-30-2013, 12:05 AM
  4. Replies: 3
    Last Post: 01-28-2012, 03:41 PM
  5. How to compare partial match in two columns
    By martinaquan in forum Excel General
    Replies: 2
    Last Post: 08-25-2010, 02:15 PM

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