VBA Arrays for concatenate?

twelin

New Member
Joined
May 10, 2019
Messages
15
Hi all,
I have long a list of entities with a unique code, and the countries where they are represented.
I want to discard all "GB" and then list all counties per entity in one row. See example.

I tried out with arrays but couldn't figure it out.

INPUT
Desired Output
ISIN
Country
ISIN
Countries
SE0000900169
GB
SE0000900169
SE
SE0000900169
SE
SE0008103071
NO, SE
SE0008103071
GB
SE0011338441
FI, LU, SE
SE0008103071
NO
SE0008103071
NO
SE0008103071
SE
SE0011338441
FI
SE0011338441
FI
SE0011338441
GB
SE0011338441
LU
SE0011338441
SE

Does anyone have a good idea on how to perform this?

Regards
Tobias
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
How about
Code:
Sub twelin()
   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
         If Cl.Value <> "GB" Then
            If Not .Exists(Cl.Offset(, -1).Value) Then
               .Add Cl.Offset(, -1).Value, Cl.Value
            Else
               .Item(Cl.Offset(, -1).Value) = .Item(Cl.Offset(, -1).Value) & ", " & Cl.Value
            End If
         End If
      Next Cl
      Range("D2").Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
   End With
End Sub
 
Upvote 0
Solution
If you do not want duplicate countries, modify the Else statement in Fluff's code to this:
Code:
ElseIf InStr(1, .Item(Cl.Offset(, -1).Value), Cl.Value, vbTextCompare) = 0 Then
     .Item(Cl.Offset(, -1).Value) = .Item(Cl.Offset(, -1).Value) & ", " & Cl.Value
End If
 
Last edited:
Upvote 0
Thank you Fluff for such a speedy help! Fantastic!

Your code works like a charm, however CalcSux78's was right about the duplicates. When I followed his suggestion I get an error code:

Sub twelin()
Dim Cl As Range

With CreateObject("scripting.dictionary")
For Each Cl In Range("B2", Range("B" & Rows.Count).End(xlUp))
If Cl.Value <> "GB" Then
If Not .Exists(Cl.Offset(, -1).Value) Then
.Add Cl.Offset(, -1).Value, Cl.Value
ElseIf InStr(1, .Item(C1.Offset(, -1).Value), C1.Value, vbTextCompare) = 0 Then
.Item(C1.Offset(, -1).Value) = .Item(C1.Offset(, -1).Value) & ", " & C1.Value
End If
Next Cl 'Compile error: Next without For
Range("D2").Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
End With
End Sub

How can that be? There is obviously a "For Each..." 7 rows up?
 
Upvote 0
You're missing an End If, before the Next Cl
 
Upvote 0
Glad we could help & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,214,963
Messages
6,122,484
Members
449,088
Latest member
Melvetica

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