How to lookup a string within a column of substrings
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:
User Defined Functions (UDFs) are very easy to install and use:
- ALT-F11 brings up the VBE window
- ALT-I ALT-M opens a fresh module
- 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:
- bring up the VBE window as above
- clear the code out
- 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!