Solved

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

Posted on 2015-01-19
6
135 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 48

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 48

Expert Comment

by:Rgonzo1971
ID: 40557351
Example pls
0
 
LVL 48

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This article descibes how to create a connection between Excel and SAP and how to move data from Excel to SAP or the other way around.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.

760 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now