?
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
Medium Priority
?
205 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 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
This article describes a serious pitfall that can happen when deleting shapes using VBA.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This Micro Tutorial will demonstrate the scrolling table in Microsoft Excel using the INDEX function.

764 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