Lookup and match multiple values separated by commas in a cell and produce matched value

abdulrahim845

New Member
Joined
Mar 13, 2023
Messages
18
Office Version
  1. 2016
Platform
  1. Windows
C2:C7 has single non-sequential numbers and B2:B7 has multiple numbers separated by commas in each cell. I want to match the C2 numbers in B2:B7 and get the output of the corresponding matching number in D2. Can anyone help me formulate this? Thanks
 

Attachments

  • Matched Values.JPG
    Matched Values.JPG
    71.6 KB · Views: 25
Is it possible that more than 1 value will be found? I adjusted my VBA version:

Code:
Sub FindC_in_B()
Dim ary() As String, i As Long, lrb As Long, lrc As Long, u As Long, f As String
lrb = Cells(Rows.Count, "B").End(xlUp).Row
lrc = Cells(Rows.Count, "C").End(xlUp).Row

For i = 2 To lrc
f = ""
 ary() = Split(Cells(i, "C"), ", ")
  For u = LBound(ary) To UBound(ary)
   If IsNumeric(Application.Match(ary(u), Range("B2:B" & lrb), 0)) Then
   f = f & Right("0" & ary(u), 12) & ", "
   ' Cells(i, "E") = Right("0" & ary(u), 12)
    Else
   End If
  Next u
  Cells(i, "E") = Left(f, Len(f) - 2)
found: Next i
End Sub
 
Upvote 0

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
I am sorry to say, I did all as you said, but it's not working. May be some error on my end.
 
Upvote 0
Is it possible that more than 1 value will be found? I adjusted my VBA version:

Code:
Sub FindC_in_B()
Dim ary() As String, i As Long, lrb As Long, lrc As Long, u As Long, f As String
lrb = Cells(Rows.Count, "B").End(xlUp).Row
lrc = Cells(Rows.Count, "C").End(xlUp).Row

For i = 2 To lrc
f = ""
 ary() = Split(Cells(i, "C"), ", ")
  For u = LBound(ary) To UBound(ary)
   If IsNumeric(Application.Match(ary(u), Range("B2:B" & lrb), 0)) Then
   f = f & Right("0" & ary(u), 12) & ", "
   ' Cells(i, "E") = Right("0" & ary(u), 12)
    Else
   End If
  Next u
  Cells(i, "E") = Left(f, Len(f) - 2)
found: Next i
End Sub

Yes, there are some chances that we may encounter more than 1 matching value. I was about to ask, but I restained myself, thought its enough help. Anyways Thanks @kweaver
 
Upvote 0
why haven't you tried the formula in post #20?
 
Upvote 0
Did you place the macro in a MODULE for that workbook?
How is it NOT working?
I was giving remarks about this formula. Your macro works fine. I am learning excel, for my personal work to automate. It is a great opportunity to solve and learn same problem with different solution sets

=IFERROR(IF(ISNUMBER(FIND(",",$B1,1))=FALSE,INDEX($A$1:$A$2000,MATCH(B1,$A$1:$A$2000,0)),MID($B1,SUM(IFERROR(FIND($A$1:$A$2000,B1),0)),FIND(",",$B1,1+SUM(IFERROR(FIND($A$1:$A$2000,B1),0)))-SUM(IFERROR(FIND($A$1:$A$2000,B1),0)))),"")
 
Upvote 0
I made one other modification in case NONE of the values are found:

Code:
Sub FindC_in_B()
Dim ary() As String, i As Long, lrb As Long, lrc As Long, u As Long, f As String
lrb = Cells(Rows.Count, "B").End(xlUp).Row
lrc = Cells(Rows.Count, "C").End(xlUp).Row
Range("E2:E" & lrc).NumberFormat = "@"

For i = 2 To lrc
f = ""
 ary() = Split(Cells(i, "C"), ", ")
  For u = LBound(ary) To UBound(ary)
   If IsNumeric(Application.Match(ary(u), Range("B2:B" & lrb), 0)) Then
   f = f & Right("0" & ary(u), 12) & ", "
   ' Cells(i, "E") = Right("0" & ary(u), 12)
    Else
   End If
  Next u
  Cells(i, "E") = Left(f, Application.Max(0, Len(f) - 2))
  
found: Next i
End Sub
 
Upvote 0
Solution
I made one other modification in case NONE of the values are found:

Code:
Sub FindC_in_B()
Dim ary() As String, i As Long, lrb As Long, lrc As Long, u As Long, f As String
lrb = Cells(Rows.Count, "B").End(xlUp).Row
lrc = Cells(Rows.Count, "C").End(xlUp).Row
Range("E2:E" & lrc).NumberFormat = "@"

For i = 2 To lrc
f = ""
 ary() = Split(Cells(i, "C"), ", ")
  For u = LBound(ary) To UBound(ary)
   If IsNumeric(Application.Match(ary(u), Range("B2:B" & lrb), 0)) Then
   f = f & Right("0" & ary(u), 12) & ", "
   ' Cells(i, "E") = Right("0" & ary(u), 12)
    Else
   End If
  Next u
  Cells(i, "E") = Left(f, Application.Max(0, Len(f) - 2))
 
found: Next i
End Sub

Matching, more than 1 matching value and None can be done by this single macro.?
 
Upvote 0

Forum statistics

Threads
1,223,893
Messages
6,175,241
Members
452,622
Latest member
Laura_PinksBTHFT

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