Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2015-01-19
6
Medium Priority
?
149 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 52

Accepted Solution

by:
Rgonzo1971 earned 1000 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 1000 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
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!

 
LVL 52

Expert Comment

by:Rgonzo1971
ID: 40557351
Example pls
0
 
LVL 52

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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Excel can be a tricky bit of software to get your head around. Whilst you’ll be able to eventually get to grips with the basic understanding of how to get by, there are a few Excel tips that not everybody will even know about let alone know how to d…
Do you use a spreadsheet like Microsoft's Excel?  Have you ever wanted to link out to a non excel file on your computer or network drive?  This is the way I found to do it!
This Micro Tutorial demonstrate the bugs in Microsoft Excel for Mac with Pivot Charts.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

721 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