2

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

(DATA TABLE)
           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:

(TYPE TABLE)
PRODUCT SUBSTRING    |  TYPE
=====================================
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?

2 Answers 2

3

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:

=GetType(A1)

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:

=myfunction(A1)

To learn more about macros in general, see:

http://www.mvps.org/dmcritchie/excel/getstarted.htm

and

http://msdn.microsoft.com/en-us/library/ee814735(v=office.14).aspx

and for specifics on UDFs, see:

http://www.cpearson.com/excel/WritingFunctionsInVBA.aspx

Macros must be enabled for this to work!

1
  • Thanks @Gary's Student, it worked like a charm :) I was a little dicey stepping into VB scripting, but it seems to be a better way rather than jumping through a long string on function soup.
    – kumarharsh
    Dec 16, 2015 at 19:22
0

Here is a solution for your problem:

=INDEX(Table2[TYPE],MAX((ROW(Table2[PRODUCT SUBSTRING])-1)*SIGN(IFERROR(SEARCH(Table2[PRODUCT SUBSTRING],[@[REF_ID]]),0))))
This is an array formula, you need to enter it with CTRL+Shift+Enter.

where:

  • 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

Update

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:
=INDEX(G$2:G$7,MAX((ROW(F$2:F$7)-1)*SIGN(IFERROR(SEARCH(F$2:F$7,A3),0))))

2
  • Hi @Máté Juhász, thanks for your answer and explaination. I am not able to understand the [@[REF_ID]] part, and Excel is giving me some error when I try to paste your formula in the cell: The syntax of this name isn't correct
    – kumarharsh
    Dec 16, 2015 at 19:23
  • @kumar_harsh: please see my updated answer Dec 17, 2015 at 7:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .