vba help - extract duplicate via vlookup/dictionary

Mallesh23

Well-known Member
Joined
Feb 4, 2009
Messages
976
Office Version
  1. 2010
Platform
  1. Windows
Hi Team,

I want to apply vlookup and get matching value.
But if there are duplicate matching value I want to show all by using Seperator.


Below is a Table
Input Column A and B.
with expected output Column E and F. how to achieve below task using dictionary or any other method.

Book1
ABCDEF
1NameBank Ac NameExpected Output:=>NameBank Ac Name
2SachinSBISachinSBI/HDFC
3DhoniSBIDhoniSBI/JP Morgan
4SachinHDFCSachinSBI/HDFC
5KohliICICIKohliICICI/SWISS/Bank of America
6GayleSWISSGayleSWISS
7PetersonHSBCPetersonHSBC
8PontingBank of AmericaPontingBank of America
9DhoniJP MorganDhoniSBI/JP Morgan
10JaysuryaBank of ChinaJaysuryaBank of China/HSBC
11JaysuryaHSBCJaysuryaBank of China/HSBC
12KohliSWISSKohliICICI/SWISS/Bank of America
13KohliBank of AmericaKohliICICI/SWISS/Bank of America
Sheet1



Thanks
mg
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
What have you already tried?
 
Upvote 0
Try this
VBA Code:
Sub Test()
    Dim a, i As Long
    a = Sheets("Sheet1").Cells(1).CurrentRegion.Value
    With CreateObject("Scripting.Dictionary")
        For i = 2 To UBound(a, 1)
            If Not .Exists(a(i, 1)) Then
                a(.Count + 1, 1) = a(i, 1): a(.Count + 1, 2) = Empty
                .Item(a(i, 1)) = Array(.Count + 1, CreateObject("Scripting.Dictionary"))
                .Item(a(i, 1))(1).CompareMode = 1
            End If
            If Not .Item(a(i, 1))(1).Exists(a(i, 2)) Then
                a(.Item(a(i, 1))(0), 2) = a(.Item(a(i, 1))(0), 2) & IIf(a(.Item(a(i, 1))(0), 2) <> "", " / ", "") & a(i, 2)
                .Item(a(i, 1))(1)(a(i, 2)) = Empty
            End If
        Next i
        i = .Count
    End With
    Sheets("Sheet1").Cells(7).Resize(i, 2) = a
End Sub
 
Upvote 0
Hi Fluff,

This is my attempted code, It bring only first Match.

VBA Code:
Sub Dict_array()
    
    Dim dict As New Scripting.dictionary
    Dim arr As Variant
    Dim i As Long
    Dim ary As Variant
   
    arr = Range("A1").CurrentRegion.Value
   
    'Store in Dictionary
    With dict
        For i = LBound(arr, 1) To UBound(arr, 1)
            If Not .Exists(arr(i, 1)) Then
                .Add (arr(i, 1)), arr(i, 2)
            End If
        Next i
   
       ary = Range("E2:E13").Value2
     
       For i = LBound(ary, 1) To UBound(ary, 1)
             ary(i, 1) = .Item(ary(i, 1))
       Next i
   
    End With
    Range("f2:f13").Value = ary
End Sub


Thanks
mg
 
Upvote 0
Ok, try it like
VBA Code:
Sub Dict_array()
    
    Dim dict As New Scripting.dictionary
    Dim arr As Variant
    Dim i As Long
    Dim ary As Variant
   
    arr = Range("A1").CurrentRegion.Value
   
    'Store in Dictionary
    With dict
        For i = LBound(arr, 1) To UBound(arr, 1)
            If Not .Exists(arr(i, 1)) Then
                .Add (arr(i, 1)), arr(i, 2)
            Else
                .Item(arr(i, 1)) = .Item(arr(i, 1)) & "/" & arr(i, 2)
            End If
        Next i
   
       ary = Range("E2:E13").Value2
     
       For i = LBound(ary, 1) To UBound(ary, 1)
             ary(i, 1) = .Item(ary(i, 1))
       Next i
   
    End With
    Range("f2:f13").Value = ary
End Sub
 
Upvote 0
Hi Fluff and Yasir,

Perfect ! it worked , Millions of Thanks both of you for your help. ?

I will choose Fluffs answer that was I am expecting in current task.
Yasir - your answer is also correct you have taken unique Names, I was looking all. Thanks



Thanks
mg
 
Upvote 0
Glad we could help & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,214,909
Messages
6,122,189
Members
449,072
Latest member
DW Draft

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