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.

[TABLE="width: 0"]
[TR]
[TD]INPUT
[/TD]
[TD][/TD]
[TD][/TD]
[TD]Desired Output
[/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]ISIN
[/TD]
[TD]Country
[/TD]
[TD][/TD]
[TD]ISIN
[/TD]
[TD]Countries
[/TD]
[/TR]
[TR]
[TD]SE0000900169
[/TD]
[TD]GB
[/TD]
[TD][/TD]
[TD]SE0000900169
[/TD]
[TD]SE
[/TD]
[/TR]
[TR]
[TD]SE0000900169
[/TD]
[TD]SE
[/TD]
[TD][/TD]
[TD]SE0008103071
[/TD]
[TD]NO, SE
[/TD]
[/TR]
[TR]
[TD]SE0008103071
[/TD]
[TD]GB
[/TD]
[TD][/TD]
[TD]SE0011338441
[/TD]
[TD]FI, LU, SE
[/TD]
[/TR]
[TR]
[TD]SE0008103071
[/TD]
[TD]NO
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SE0008103071
[/TD]
[TD]NO
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SE0008103071
[/TD]
[TD]SE
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SE0011338441
[/TD]
[TD]FI
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SE0011338441
[/TD]
[TD]FI
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SE0011338441
[/TD]
[TD]GB
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SE0011338441
[/TD]
[TD]LU
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]SE0011338441
[/TD]
[TD]SE
[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[/TABLE]

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

Regards
Tobias
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
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,224,823
Messages
6,181,176
Members
453,021
Latest member
Justyna P

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