Find Text With INDEX and MATCH

Find Text With INDEX and MATCH

Is there a harder working team in Excel, than the reliable duo of INDEX and MATCH? These functions work beautifully together, with MATCH identifying the location of an item, and INDEX pulling the results out from the murky depths of data. See how to find text with INDEX and MATCH.

Find Text in a String

Last week, Jodie asked if I could help with a problem, and INDEX and MATCH came to the rescue again.

Jodie sent me a picture of her worksheet, with text strings in column A and codes in column D. Each text string contained one of the codes, and Jodie wanted that code to appear in column B.

Would you use INDEX and MATCH to find the code, or another method? Keep reading to see my solution, and please share your ideas, if you have other ways to solve this.

indexmatchtextstring01

Count the Occurrences With COUNTIF

When you want to find text that’s buried somewhere in a string, the * wildcard character is useful. We can use the wildcard with COUNTIF, to see if the string is found somewhere in the text.

I entered this test formula in cell B1. This formula needs to be array-entered, so press Ctrl + Shift + Enter.

=COUNTIF(A1,”*” & $D$1:$D$3 & “*”)

indexmatchtextstring02
There are wildcard characters before and after the cell references to D1:D3, so the text will be found anywhere within the text string.

To see the results of the array formula, click in the formula bar, and press the F9 key. The array shows 0;1;0 so it found a match for CAT, which is in the second cell in the range, $D$1:$D$3.

  • Important: After you check the results, press the Esc key, to exit the formula without saving the calculated results.

indexmatchtextstring03

Get the Position With MATCH

Next, you can add the MATCH function, wrapped around the COUNTIF formula, to get the position of the “1” in the results.

Make the following change to the formula in cell B1, and remember to press Ctrl + Shift + Enter.

=MATCH(1,COUNTIF(A1,”*”&$D$1:$D$3&”*”),0)

indexmatchtextstring04

The result is 2, so the code “CAT”, the 2nd item in range D1:D3, was found in cell A1.

Get the Code With INDEX

Next, the INDEX function can return the code from the range $D$1:$D$3, that is at the position that the MATCH function identified.

Make the following change to the formula in cell B1, and remember to press Ctrl + Shift + Enter.

=INDEX($D$1:$D$3,MATCH(1,COUNTIF(A1,”*”&$D$1:$D$3&”*”),0))

indexmatchtextstring05

The result is CAT, so the formula is working correctly.

Prevent Error Results With IFERROR

There should be one valid code in each text string, but sometimes the data doesn’t cooperate. Just in case there are text strings without a code, or more than one instance of the code, you can use IFERROR to show an empty string, instead of an error. (Excel 2007 and later versions)

=IFERROR(INDEX($D$1:$D$3,MATCH(1,COUNTIF(A1,”*”&$D$1:$D$3&”*”),0)),””)

Enter with Ctrl + Shift + Enter, and then copy the formula down to row 10.

indexmatchtextstring06

In cell B6, the formula returns an empty string, and the cell looks blank, because none of the valid codes are in the text that’s in cell A6.

Use a Named Range

Instead of referring to range $D$1:$D$3, you could name that range, and use the name in the INDEX/MATCH formula. That would make it easier to maintain, if the size of the codes list will change.

indexmatchtextstring07

Download Find Text With INDEX and MATCH Sample File

To get the sample file, and see how the formula works, go to the INDEX and MATCH page on my Contextures site.

In the Download section on that page, look for sample file 4 – Find Text From Code List. The zipped file is in xlsx format, and does not contain macros.

More INDEX and MATCH Examples

There are more examples of using INDEX and MATCH on my Contextures site.

For example, this video shows how to use INDEX and MATCH to find the best price.

____________________________

Find Text With INDEX and MATCH

58 thoughts on “Find Text With INDEX and MATCH”

  1. Is there a way to get the text instead of the formula in column B in the first example.
    Thank you!!!
    Jorge

  2. Hello,
    I just want to make a comment:
    I have seen many ways to solve a problem but THERE IS NOTHING explained as this one.
    THUIS IS THE BEST I’VE EVER SEEN IN 20 YEARRS!!!
    Thank you so much!!!
    Jorge Cue

  3. Hi Debra,
    I have tried using the get code with index but excel seems to struggle over 20k rows where string within each cell can be quite long. Can you suggest any alternatives how this formula can work quickly? Many thanks

  4. Hi Debra.
    Am I right in thinking that the formula =COUNTIF(A1,”*” & $D$1:$D$3 & “*”) counts the number of cells that contain at least one instance of the codes? Not the number of times the codes appear in each cell?
    Any suggestions on how to count the number of times the codes in Column D appear in Column A when the codes can appear multiple times in the same cell?
    A1: CAT ABC CAT
    A2: XYZ CAT
    Expected result: 3
    Thanks.

  5. Can anyone here Help me on below sample data
    I have more then 1000 entries of repeating names in “data file” these names are to be replaced with codes provided in separate sheet, i have tried lookup function but its not working with this data.
    File 1 : “Data File” has this format
    Names:
    Madiha Siraj
    Nayha
    Arneeb Farooq
    Noreen Huma
    Madiha Siraj
    Shagufta Parveen
    Nayha
    File 2: “Codes File”
    Names Codes
    Dr.Nayha Kahlid 1085
    Dr.Madiha Siraj 1081
    Dr.Shagufta Parveen 1010
    Dr.Arneeb Farooq 1009
    Dr.Noreen Huma 1002

  6. Hi Debra, can you provide an spreadsheet where I can see the formulas working? Step 2 “Count the Occurrences With COUNTIF” does not work at all for me. I do always get the excel error #NV, which seems to be an inconsistent cell format

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.