Solved

How to create a vba code to search a string for values on a list and return with the value found?

Posted on 2014-12-09
3
203 Views
Last Modified: 2014-12-10
I uploaded an example file with 4 different text strings in column “A” and the desired result in column “B” on sheet1.  The list of values to search for is found in cells on sheet2.
Exampe.xlsx
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
  • 2
3 Comments
 
LVL 27

Accepted Solution

by:
Glenn Ray earned 500 total points
ID: 40490094
Here is a user-defined function that will work exactly as you requested:
Public Function Find_Matches(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
            Find_Matches = Find_Matches & cl.Value & ", "
        End If
    Next cl
    If Find_Matches <> "" Then
        Find_Matches = Left(Find_Matches, Len(Find_Matches) - 2)
    Else
        Find_Matches = "NOT FOUND"
    End If
End Function

Open in new window


In cell B2 you would enter:
=find_matches(A2,Sheet2!$A$2:$A$5)

and you could then copy that to each cell below.  

If more than one match is found, they are all shown - separated by commas.

Regards,
-Glenn
0
 

Author Closing Comment

by:kbay808
ID: 40490528
Perfect!!!  Thank you very much.
0
 
LVL 27

Expert Comment

by:Glenn Ray
ID: 40491700
You're welcome.  You'd be surprised how often I use this UDF myself.

-Glenn
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

This article will guide you to convert a grid from a picture into Excel format using Microsoft OneNote and no other 3rd party application.
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 will demonstrate the scrolling table in Microsoft Excel using the INDEX function.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

730 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