Solved

How do I fix my macro to search for a pattern within a string in excel?

Posted on 2015-01-19
6
140 Views
Last Modified: 2015-01-19
The below code creates a function that searches for a list of values in a string, but it returns all of the values found.  I only want it to return with the first value found.
Public Function FindMatches(Test_Cell As Range, Lookup_array As Range) As String
    'This returns all substring matches within a cell
    Dim cl As Object
    For Each cl In Lookup_array
        If InStr(1, Test_Cell, cl.Value, vbTextCompare) > 0 Then
            FindMatches = FindMatches & cl.Value & ", "
        End If
    Next cl
    If FindMatches <> "" Then
        FindMatches = Left(FindMatches, Len(FindMatches) - 2)
    Else
        FindMatches = ""
    End If
End Function

Open in new window

0
Comment
Question by:kbay808
  • 3
  • 2
6 Comments
 
LVL 50

Accepted Solution

by:
Rgonzo1971 earned 250 total points
ID: 40557203
Hi,

pls try

Public Function FindMatches(Test_Cell As Range, Lookup_array As Range) As String
    'This returns all substring matches within a cell
    FindMatches = ""
    Dim cl As Object
    For Each cl In Lookup_array
        If InStr(1, Test_Cell, cl.Value, vbTextCompare) > 0 Then
            FindMatches = cl.Value
            Exit For
        End If
    Next cl
End Function

Open in new window

Regards
0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 250 total points
ID: 40557228
Public Function FindMatches(Test_Cell As Range, Lookup_array As Range) As String
    'This returns all substring matches within a cell
    Dim cl As Object
    Dim flag As Boolean
    flag = False
     
    For Each cl In Lookup_array
    If flag = False Then
        If InStr(1, Test_Cell, cl.Value, vbTextCompare) > 0 Then
           FindMatches = FindMatches & cl.Value & ", "
         
         flag = True
        End If
       
        End If
    Next cl
    If flag = True Then
   
   
FindMatches = Left(FindMatches, Len(FindMatches) - 2)
End If

End Function
0
 

Author Comment

by:kbay808
ID: 40557287
Rgonzo1971/Pratima Pharande
Both of your codes returns with the last result found in the string.  I need the first result and then stop looking.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40557351
Example pls
0
 
LVL 50

Expert Comment

by:Rgonzo1971
ID: 40557383
Maybe

Public Function FindMatches(Test_Cell As Range, Lookup_array As Range) As String
    'This returns all substring matches within a cell
    FindMatches = ""
    Pos = 10000000
    Dim cl As Object
    For Each cl In Lookup_array
        If cl <> "" And InStr(1, Test_Cell, cl.Value, vbTextCompare) > 0 Then
            Tmp = cl.Value
            PosTmp = WorksheetFunction.Find(Tmp, Test_Cell.Value)
            If PosTmp < Pos Then
                Res = cl.Value
                Pos = PosTmp
            End If
        End If
    Next cl
    FindMatches = Res
End Function

Open in new window

Regards
0
 

Author Closing Comment

by:kbay808
ID: 40557483
Both of your solutions do work.  For some reason it was not working with my test data set, but they worked just fine for the rest.  Thank you very much.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
The viewer will learn how to use the =DISCRINV command to create a discrete random variable, use this command to model a set of probabilities and outcomes in a Monte Carlo simulation, and learn how to find the standard deviation of a set of probabil…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa‚Ķ

827 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question