Lookup and Join inquiry

rn119

New Member
Joined
Feb 27, 2013
Messages
47
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?
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,514
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:

DanteAmor

Well-known Member
Joined
Dec 3, 2018
Messages
8,818
Office Version
2007
Platform
Windows
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>
 

Forum statistics

Threads
1,081,981
Messages
5,362,535
Members
400,679
Latest member
alecalec202

Some videos you may like

This Week's Hot Topics

  • populate from drop list with multiple tables
    Hi All, i have a drop list that displays data, what i want is when i select one of those from the list to populate text from different tables on...
  • Find list of words from sheet2 in sheet1 before a comma and extract text vba
    Hi Friends, Trying to find the solution on my task. But did not find suitable one to the need. Here is my query and sample file with details...
  • Dynamic Formula entry - VBA code sought
    Hello, really hope one of you experts can help with this - i've spent hours on this and getting no-where. .I have a set of data (more rows than...
  • Listbox Header
    Have a named range called "AccidentsHeader" Within my code I have: [CODE]Private Sub CommandButton1_Click() ListBox1.RowSource =...
  • Complex Heat Map using conditional formatting
    Good day excel world. I have a concern. Below link have a list of countries that carries each country unique data. [URL...
  • Conditional formatting
    Hi good morning, hope you can help me please, I have cells P4:P54 and if this cell is equal to 1 then i want row O to say "Fully Utilised" and to...
Top