Solved

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

Posted on 2015-01-19
6
144 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 51

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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 51

Expert Comment

by:Rgonzo1971
ID: 40557351
Example pls
0
 
LVL 51

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction This Article briefly covers methods of calculating the NPV and IRR variants in Excel as well as the limitations in calculating and interpreting IRR results. Paraphrasing Richard Shockley, author of my favourite finance reference tex…
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…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

729 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