Searching for a range of values within a range of values

Posted on 2014-08-18
Last Modified: 2014-08-18
I have an excel sheet Sheet1 with a set of text data values (20-50 words) in column A.
An excel sheet Sheet2 stores a set of keywords in Col. A.

I need a macro that loops through each value in Sheet1/Col.1, and searches for each values listed in Sheet2/Col.A. If it finds one or more values, the one or more values should be written in Sheet1/Col.B, If the macro does not find any values, the corresponding Cell. in Sheet1/Col. B should display not found.



ColA/Sheet 1
A1 = "This is a test string"
A2 = "this is another value"


ColA/Sheet 1
A1 = "This is a test string", B1 = "test, string"
A2 = "this is another value", B2 = "NOT FOUND"

Question by:cyber-33
    LVL 22

    Expert Comment

    by:Matt V
    You should be able to do that with VLOOKUP

    Here is a good walk-through that should help you.  Using VLOOKUP, SMALL and INDEX to return multiple values on a single lookup.

    Author Comment

    The example you provided shows vlookup to search for a single value in an array of records. In the problem I described above, I have a dynamic array of values that I need to search for. The idea is to be able to type keywords in column A of the sheet2 and have column B in sheet1 identify any existing keywords in the corresponding column a value. So, I don't see how vlookup can do it.
    LVL 27

    Accepted Solution

    The following user-defined function (UDF) will produce the results you want:
    Function Find_Matches(Test_Cell As Range, Lookup_array As Range) As String
        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)
            Find_Matches = "NOT FOUND"
        End If
    End Function

    Open in new window

    You just insert this into a module in your workbook and then call it like a regular Excel function like so:

    I've attached an example file for you to try and test.


    Author Closing Comment

    Neat! Thank you, Glenn!
    LVL 27

    Expert Comment

    by:Glenn Ray
    You're welcome.  I'm gonna hang onto that function; it might be handy.


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Suggested Solutions

    Title # Comments Views Activity
    Excel file corrupted. 13 28
    vba code not trapping out null filters 5 27
    Excel formula 5 31
    Import csv files to MS SQL 5 34
    How to quickly and accurately populate Word documents with Excel data, charts and images (including Automated Bookmark generation) David Miller (dlmille) Synopsis In this article you’ll learn how to use ExcelToWord! to copy data,charts, shapes …
    Workbook link problems after copying tabs to a new workbook? David Miller (dlmille) Intro Have you either copied sheets to a new workbook, and after having saved and opened that workbook, you find that there are links back to the original sou…
    This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    779 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

    14 Experts available now in Live!

    Get 1:1 Help Now