Lookup and Join inquiry

rn119

New Member
Joined
Feb 27, 2013
Messages
49
I need to join a comma delimited list to a list that has the corresponding naming conventions. For example...

Table A - Starting from Cell A2

45,745
209,65,35
45,112,N60

<tbody>
</tbody>


Table B - Starting from Cell A2 (ID) & B2 (Name)

45Exam One
209Test Two
112Protocol
209Demo
35Prod
65Proof of Control
745Modality
N60Culture

<tbody>
</tbody>

My results in another column should be:

Exam One, Modality
Demo, Proof of Control, Prod
Exam One, Protocol, Culture

<tbody>
</tbody>

<tbody>
</tbody>

Can this be done?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Start by formatting column A in Table A as 'Text'. The macro assumes that Table A is in Sheet1 and Table B is in Sheet2. Change the sheet names (in red) to suit your needs. The result will be place in column C of Sheet1.
Code:
Sub LookupAndJoin()
    Application.ScreenUpdating = False
    Dim LastRow As Long, srcWS As Worksheet, desWS As Worksheet, rng As Range, i As Long, spl As Variant, fnd As Range, x As Long: x = 2
    Set desWS = Sheets("[COLOR="#FF0000"]Sheet1[/COLOR]")
    Set srcWS = Sheets("[COLOR="#FF0000"]Sheet2[/COLOR]")
    LastRow = desWS.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    For Each rng In desWS.Range("A2:A" & LastRow)
        spl = Split(rng, ",")
        For i = LBound(spl) To UBound(spl)
            Set fnd = srcWS.Range("A:A").Find(spl(i), LookIn:=xlValues, lookat:=xlPart)
            If Not fnd Is Nothing Then
                If desWS.Cells(x, 3) = "" Then
                    desWS.Cells(x, 3) = fnd.Offset(, 1)
                Else
                    desWS.Cells(x, 3) = desWS.Cells(x, 3) & ", " & fnd.Offset(, 1)
                End If
            End If
        Next i
        x = x + 1
    Next rng
    Application.ScreenUpdating = True
End Sub
 
Last edited:
Upvote 0
I show you another approach, maybe it can be faster.

Code:
Sub Join_Name()
  Dim a As Variant, b As Variant, c() As Variant, dict As Object, i As Long, s As Variant
  a = Sheets("[COLOR=#ff0000]Tab1[/COLOR]").Range("[COLOR=#0000ff]A2[/COLOR]", Sheets("[COLOR=#ff0000]Tab1[/COLOR]").Range("[COLOR=#0000ff]A[/COLOR]" & Rows.Count).End(xlUp))
  b = Sheets("[COLOR=#ff0000]Tab2[/COLOR]").Range("[COLOR=#0000ff]A2[/COLOR]", Sheets("[COLOR=#ff0000]Tab2[/COLOR]").Range("[COLOR=#0000ff]B[/COLOR]" & Rows.Count).End(xlUp))
  Set dict = CreateObject("scripting.dictionary")
  For i = 1 To UBound(b)
    dict.Add CStr(b(i, 1)), b(i, 2)
  Next
  ReDim c(UBound(a) - 1)
  For i = 1 To UBound(a)
    For Each s In Split(a(i, 1), ",")
      If dict.Exists(Trim(s)) Then
        c(i - 1) = c(i - 1) & ", " & dict(Trim(s))
      End If
    Next
    c(i - 1) = Mid(c(i - 1), 3)
  Next
  Sheets("[COLOR=#ff0000]Tab1[/COLOR]").Range("[COLOR=#0000ff]B2[/COLOR]").Resize(UBound(a)).Value = Application.Transpose(c)
End Sub


Note:
In your example you have twice the 209:
209Test Two
112Protocol
209Demo

<tbody>
</tbody>
 
Upvote 0

Forum statistics

Threads
1,214,922
Messages
6,122,281
Members
449,075
Latest member
staticfluids

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