How to lookup a string within a column of substrings

Posted on


I have a data file, where there is a table like this:

           REF_ID              |     COST    |   TYPE
123_COMPUTER_XYZ               | 50000       | (to be filled)
45623_LAPTOP_AOS               | 90000
BHJ_WALLET_ASODO               | 1000
BUIGQ32_TRIMMER                | 200

Now, I want to add another column to the table, wherein the type of the product is automatically put using a lookup table, which I have defined as follows:

COMPUTER             | computer
LAPTOP               | computer
WALLET               | personal
TRIMMER              | personal-care

I want to use the PRODUCT SUBSTRING column in the TYPE TABLE as a lookup and put the corresponding value of TYPE into the
3rd column of the DATA TABLE.

I’ve looked at many questions:

but all of them ask it the other way round, wherein the string to be searched (REF_ID field) is a sub-string of the TYPE-TABLE.

Can you help me?


Here is a solution based on a short UDF().

Say the lookup table is in columns F and G from row 1 up to a maximum of 100 rows. First insert this UDF() in a standard module:

Public Function GetType(sIN As String) As String
   Dim LookupTable As Range, nItems As Long
   Set LookupTable = Range("F1:G100")
   nItems = 100

   For i = 1 To nItems
      If LookupTable(i, 1) = "" Then Exit For
      If InStr(1, sIN, LookupTable(i, 1)) > 0 Then
         GetType = LookupTable(i, 2)
         Exit Function
      End If
   Next i
   GetType = "UN-FOUND"
End Function

Then in cell C1, enter:


and copy down:

enter image description here

User Defined Functions (UDFs) are very easy to install and use:

  1. ALT-F11 brings up the VBE window
  2. ALT-I
    ALT-M opens a fresh module
  3. paste the stuff in and close the VBE window

If you save the workbook, the UDF will be saved with it.
If you are using a version of Excel later then 2003, you must save
the file as .xlsm rather than .xlsx

To remove the UDF:

  1. bring up the VBE window as above
  2. clear the code out
  3. close the VBE window

To use the UDF from Excel:


To learn more about macros in general, see:


and for specifics on UDFs, see:

Macros must be enabled for this to work!

Here is a solution for your problem:

This is an array formula, you need to enter it with CTRL+Shift+Enter.


  • IFERROR(SEARCH(Table2[PRODUCT SUBSTRING],[@[REF_ID]]),0)) checks which substring is included in the actual REF_ID (gives starting position for it, 0 for all other substrings)
  • SIGN(...) – converts previous list to 1 for found substring
  • (ROW(Table2[PRODUCT SUBSTRING])-1) – row number minus one (position in the list if header is in first row)
  • MAX(...) – as only value for the found substring is non zero, this function extracts it’s position
  • =INDEX(Table2[TYPE],MAX(...)) – type from the list

enter image description here


Above formula refers to tables, to convert your ranges to tables select: Insert – Table (in the example table1 is the one at the left side, so convert it first)

Formula with standard references:

Leave a Reply

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