Excel find xlPart Issue

Viewed 33

I'm trying to find partial matches against any text string in the Target cell to any text string in a Cell in a range and I'm using this code below, but it simply doesn't work when I expand the Cell string to longer text string separated by spaces.

If it's a single text value, like "wal" for Wal-mart, then it finds the partial text string in the Target, i.e. " WAL-MART SUPERCENTER ", but when I have "wal otherstore" in the Cell it doesn't find Wal-mart anymore. I thought that is what xlPart was supposed to do ...

Set Found = Target.Find(What:=Cell, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)

The idea is to have a list of accounts in a budget spreadsheet with associated keywords, so that the Macro then finds keywords, like "wal" and automatically assigns the Target entry to that account. The keywords are all in a single cell next to the accounts list, and the keywords are separated by a space - so it can become quite a long text string.

I'm not sure why xlPart is not working.

Full code:

Private Sub Worksheet_Change(ByVal Target As Range)

Application.ScreenUpdating = False

Dim Rng1 As Range, Rng2 As Range, Rng3 As Range, Cell As Range, Found As Range
Dim List As String
Dim Qty As Integer
Dim Coll As Collection
Dim i As Long

LastRowA = Sheets("Transactions").Cells(Rows.Count, "A").End(xlUp).Row
LastRowL = Sheets("Transactions").Cells(Rows.Count, "L").End(xlUp).Row
List = "Reset Options"
Qty = 0

Set Coll = New Collection
Set Rng1 = Sheets("Transactions").Range("G3:G" & LastRowA)
Set Rng2 = Sheets("Accounts & Budget").Range("I5:I104")
Set Rng3 = Sheets("Transactions").Range("L3:L" & LastRowL)

If Application.Intersect(Target, Rng1) Is Nothing Then
    ElseIf Application.Intersect(Target, Rng1).Address = Target.Address Then
        If Target = "" Then
                    Application.EnableEvents = False
                    Target.Offset(0, 5).Validation.Delete
                    Cells(Target.Row, "G") = ""
                    Cells(Target.Row, "L") = ""
                    Application.EnableEvents = True
        ElseIf Target <> "" Then
                        For Each Cell In Rng2
                                Set Found = Target.Find(What:=Cell, LookIn:=xlValues, LookAt:=xlPart, MatchCase:=False)
                                If Found Is Nothing Then
                                Else
                                    Qty = Qty + 1
                                    Coll.Add Cell.Offset(0, -2)
                                End If
                        Next Cell
            If Qty = 1 And Coll.Count = 1 Then
                Application.EnableEvents = False
                Target.Offset(0, 5).Validation.Delete
                Target.Offset(0, 5) = Coll(1)
                Target.Offset(0, 5).Validation.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:="=Accounts"
                Application.EnableEvents = True
            ElseIf Qty = 0 And Coll.Count = 0 Then
                Application.EnableEvents = False
                Target.Offset(0, 5).Validation.Delete
                Target.Offset(0, 5) = "No Results - Select From List"
                Target.Offset(0, 5).Validation.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:="=Accounts"
                Application.EnableEvents = True
            ElseIf Qty > 1 And Coll.Count > 1 Then
                Application.EnableEvents = False
                Target.Offset(0, 5).Validation.Delete
                Target.Offset(0, 5) = "Multiple Results - Select From List"
                    For i = 1 To Coll.Count
                       List = List & "," & Coll(i)
                    Next i
                Target.Offset(0, 5).Validation.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:=List
                Application.EnableEvents = True
            End If
        End If
End If

If Application.Intersect(Target, Rng3) Is Nothing Then
    ElseIf Application.Intersect(Target, Rng3).Address = Target.Address Then
        If Target = "Reset Options" Or Target = "" Then
            Application.EnableEvents = False
            Target.Validation.Delete
            Target.Validation.Add Type:=xlValidateList, Operator:=xlBetween, Formula1:="=Accounts"
            Application.EnableEvents = True
            Target = "Select From List"
        ElseIf Target.Offset(0, -5) = "" Then
            Application.EnableEvents = False
            Target.Validation.Delete
            Target = ""
            Application.EnableEvents = True
        End If
End If

Application.ScreenUpdating = True
End Sub
0 Answers
Related