Find all Partial matches in Sheet 1 Column D that match names in Sheet 2 Column A

westc4

Board Regular
Joined
Aug 1, 2012
Messages
89
Office Version
  1. 365
Platform
  1. Windows
Hello,

I am trying to tweak the following VBA code. What I need to happen is for excel to look at the list of names in Column "A" Sheet "Name" and highlight all cells in Column "D" on Sheet "Match" that match the "A" list, a partial name match should be highlighted.

Both the sheets ("Name" and "Match") can have a different number of rows that can vary, so I also need to make sure it searches all names and reviews for all possible matches.

It currently is not finding anything, though there are a number or direct matches and more than 1,000 partial matches.

Code:
Sub CheckNames()
Dim Cell As Range, cRange As Range, sRange As Range, Rng As Range, FindString As String

LastRow1 = Sheets("Match").Cells(Rows.Count, "D").End(xlUp).Row
LastRow2 = Sheets("Name").Cells(Rows.Count, "A").End(xlUp).Row

Set cRange = Sheets("Match").Range("D3:D" & LastRow1)
Set sRange = Sheets("Name").Range("A1:A" & LastRow2)

    For Each Cell In cRange
        FindString = Cell.Value
    With sRange
         Set Rng = .Find(What:=FindString, _
                    After:=.Cells(1), _
                    LookIn:=xlValues, _
                    LookAt:=xlPart, _
                    SearchOrder:=xlByRows, _
                    SearchDirection:=xlPrevious, _
                    MatchCase:=False)
            If Not Rng Is Nothing Then
                Rng.Interior.ColorIndex = 6
                    Cell.Interior.ColorIndex = 6
            End If
    End With
    Next
End Sub
 
I think to speed it up we can use a string to build the range to be colored - checking if the string has less than 255 characters

Something like this

Code:
Sub aTest()
    Dim strRange As String, LastRow1 As Long, LastRow2 As Long
    Dim rName As Range, rSearch As Range, rn As Range, rs As Range
    
    LastRow1 = Sheets("Match").Cells(Rows.Count, "D").End(xlUp).Row
    LastRow2 = Sheets("Name").Cells(Rows.Count, "A").End(xlUp).Row
    
    Set rSearch = Sheets("Match").Range("D2:D" & LastRow1)
    Set rName = Sheets("Name").Range("A2:A" & LastRow2)

    Sheets("Name").Activate
    For Each rn In rName
        For Each rs In rSearch
            If UCase(rs.Value) Like "*" & UCase(rn.Value) & "*" Then
                If Len(strRange) + Len(rn.Address(False, False)) < 255 Then
                    strRange = strRange & "," & rn.Address(False, False)
                Else
                    rn.Interior.Color = vbYellow
                    Range(Mid(strRange, 2)).Interior.Color = vbYellow
                    strRange = ""
                End If
            End If
        Next rs
    Next rn
    If Len(strRange) Then Range(Mid(strRange, 2)).Interior.Color = vbYellow
                    
End Sub

M.
 
Upvote 0

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Joe, you are definitely right on that. My preferred method is using arrays to store data and send changes back as a whole. Processing 100,000+ rows of data in fractions of a second. Without a full data set (I realize it's not feasible or secure for this scenario), it's hard to adjust for calculation performance.
 
Upvote 0

Forum statistics

Threads
1,215,474
Messages
6,125,023
Members
449,203
Latest member
tungnmqn90

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