Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
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
?
206 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: 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

Some code to ensure data integrity when using macros within Excel. Also included code that helps secure your data within an Excel workbook.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

636 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