Look up Number in a list and return matching text

kishorenaidugurram

New Member
Joined
Jun 23, 2015
Messages
11
Office Version
  1. 2016
  2. 2013
Platform
  1. Windows
Lookup.xlsx
IJKLMN
2To be Looked intoOutputNameID
3ABC(1234),FGH(8976),EFG(4567)ABCLooking or a Formula in J3ABC1234
4FGH(8976),EFG(45678),CDE(3456)CDELooking or a Formula in J4BCD2345
5CDE3456
6
7Need to look values in Col N in Col I and if matching found return the associated name
8Ex: 1234 is in Cell I3 and return corresponding text i.e, ABC
Sheet2
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Like this? A formula and a VBA version.

20210509 Stat Pull Chacon Encounters.xlsm
ABCDE
1To be Looked intoVBAO365ID
2ABC(1234),FGH(8976),EFG(4567)FGHFGH8976
3FGH(8976),EFG(45678),CDE(3456)CDECDE3456
4ABC(1234),FGH(8976),EFG(4567)ABCABC1234
5FGH(8976),EFG(45678),CDE(3456)#N/A#N/A9999
Sheet2
Cell Formulas
RangeFormula
C2:C5C2=XTRACT(A2,E2)
D2:D5D2=LET(tbl,FILTERXML("<a><b>"&SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,")",""),"(",","),",","</b><b>")&"</b></a>","//b"),r,ROWS(tbl),seq,SEQUENCE(r/2,2),x,INDEX(tbl,seq),INDEX(INDEX(x,,1),MATCH(E2,INDEX(x,,2),0)))


VBA Code:
Function XTRACT(s As String, lk As String)
Dim res As String: res = "N/A"
With CreateObject("VBScript.RegExp")
    .Global = True
    .Pattern = "(\w+)\((\d+)\)"
    Set matches = .Execute(s)
    For Each m In matches
        If m.submatches(1) = lk Then res = m.submatches(0): Exit For
    Next m
End With
XTRACT = res
End Function
 
Upvote 0
Hi Kishorenaidugurram,

If I can use a work column...


kishorenaidugurram.xlsx
IJKLMNO
2To be Looked intoOutputNameIDWork
3ABC(1234),FGH(8976),EFG(4567)ABCLooking or a Formula in J3ABC1234ABC
4FGH(8976),EFG(45678),CDE(3456)CDELooking or a Formula in J4BCD2345 
5 CDE3456CDE
6  
7Need to look values in Col N in Col I and if matching found return the associated name  
8Ex: 1234 is in Cell I3 and return corresponding text i.e, ABC  
9  
Sheet1 (2)
Cell Formulas
RangeFormula
J3:J9J3=IFERROR(INDEX($O$3:$O$9999,AGGREGATE(15,6,ROW($O$3:$O$9999)-ROW($O$2)/(O$3:O$9999<>""),ROW()-ROW($J$2))),"")
O3:O9O3=IFERROR(MID(INDEX($I$3:$I$9999,AGGREGATE(15,6,ROW($N$3:$N$9999)-ROW($N$2)/(ISNUMBER(SEARCH("("&N3&")",$I$3:$I$9999))),1)),SEARCH("("&N3&")",INDEX($I$3:$I$9999,P3))-3,3),"")
 
Upvote 0
It would help if you indicated what version of Excel you run. If you don't run 365, lrobbo has wasted his time.
 
Upvote 0
Another alternative:

Book3
IJKLMN
1To be Looked intoOutputNameID
2ABC(1234),FGH(8976),EFG(4567)ABC1234
3FGH(8976),EFG(45678),CDE(2345)CDE2345
4XXX(8976),YYYY(45678),Z(99)YYYY45678
5XXX(8976),YYYY(45678),Z(99)Z99
6XXX(8976),YYYY(45678),Z(99)100
Sheet4


Code:
Sub findtext()
Dim d As String, dd() As String, lr As Long
Dim i As Long, j As Long, val1 As Long
lr = Cells(Rows.Count, "I").End(xlUp).Row
For i = 2 To lr
 d = Cells(i, 9)
 d = Replace(Replace(d, "(", ","), ")", "")
 dd = Split(d, ",")
 val1 = Cells(i, 14).Value
 For j = 1 To UBound(dd) Step 2
   If val1 = dd(j) Then
   Cells(i, 10) = dd(j - 1)
   Else
   End If
 Next j
Next i
End Sub
 
Upvote 0
Another alternative:

Book3
IJKLMN
1To be Looked intoOutputNameID
2ABC(1234),FGH(8976),EFG(4567)ABC1234
3FGH(8976),EFG(45678),CDE(2345)CDE2345
4XXX(8976),YYYY(45678),Z(99)YYYY45678
5XXX(8976),YYYY(45678),Z(99)Z99
6XXX(8976),YYYY(45678),Z(99)100
Sheet4


Code:
Sub findtext()
Dim d As String, dd() As String, lr As Long
Dim i As Long, j As Long, val1 As Long
lr = Cells(Rows.Count, "I").End(xlUp).Row
For i = 2 To lr
 d = Cells(i, 9)
 d = Replace(Replace(d, "(", ","), ")", "")
 dd = Split(d, ",")
 val1 = Cells(i, 14).Value
 For j = 1 To UBound(dd) Step 2
   If val1 = dd(j) Then
   Cells(i, 10) = dd(j - 1)
   Else
   End If
 Next j
Next i
End Sub
thanks for response. Getting stuck @val1 = Cells(i, 14).Value. Also where are we using Col N in the above VBA please?
 
Upvote 0
What does "getting stuck" mean? If you're getting an error, what is the error. Column N is the 14th column which is here: val1 = Cells(i, 14).Value
 
Upvote 0
thanks for response. Getting stuck @val1 = Cells(i, 14).Value. Also where are we using Col N in the above VBA please?
Would like to check Emp. ID in column N which is unique and search for Col I and if found fetch the name of the Employee.
What does "getting stuck" mean? If you're getting an error, what is the error. Column N is the 14th column which is here: val1 = Cells(i, 14).Value
Runtime Error 13. Type Mismatch
 
Upvote 0

Forum statistics

Threads
1,214,983
Messages
6,122,583
Members
449,089
Latest member
Motoracer88

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top