Add multiple data for same name into cell next to it

Nick70

Active Member
Joined
Aug 20, 2013
Messages
299
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have fruit names in column B which have a related country of origin in column C.

Fruit names can be repeated (eg. have duplicates or more) and country of origin could be different (e.g Strawberries can have Spain but another time they can have UK as country of origin).

I would like to have a macro that adds data for each fruit name into relative cells in column D (see example below).

Book1
ABCDEF
1
2
3Fruit NamesContry of originTotal Countries of origin
4strawberriesSpainSpain UK
5applesFranceFrance Italy
6pearsSpainSpain Italy
7figsTurkeyTurkey Spain
8cherriesUKUK Italy
9orangeItalyItaly Spain
10plumsFranceFrance UK
11grapesSpainSpain South Africa
12applesItalyFrance Italy
13pearsItalySpain Italy
14figsSpainSpain
15cherriesItalyUK Italy
16orangeSpainItaly Spain
17plumsUKFrance UK
18grapesSouth AfricaSpain South Africa
19strawberriesUKSpain UK
20
21
22
23
Sheet1


How would I do that?

Thanks!
N.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Something like this.

Book1
BCDEFG
3Fruit NamesContry of originFruitCountry
4strawberriesSpainstrawberriesSpain UK
5applesFranceapplesFrance Italy
6pearsSpainpearsSpain Italy
7figsTurkeyfigsTurkey Spain
8cherriesUKcherriesUK Italy
9orangeItalyorangeItaly Spain
10plumsFranceplumsFrance UK
11grapesSpaingrapesSpain South Africa
12applesItaly
13pearsItaly
14figsSpain
15cherriesItaly
16orangeSpain
17plumsUK
18grapesSouth Africa
19strawberriesUK
Sheet1


VBA Code:
Sub combo()
Dim AR() As Variant: AR = Range("B4:C" & Range("B" & Rows.Count).End(xlUp).Row).Value
Dim SD As Object: Set SD = CreateObject("Scripting.Dictionary")

For i = 1 To UBound(AR)
    If Not SD.exists(AR(i, 1)) Then
        SD.Add AR(i, 1), AR(i, 2)
    Else
        SD(AR(i, 1)) = SD(AR(i, 1)) & " " & AR(i, 2)
    End If
Next i

With Range("F3:G3")
    .Value = Array("Fruit", "Country")
    .Font.Bold = True
End With

Range("G4").Resize(SD.Count, 1).Value = Application.Transpose(SD.items)
Range("F4").Resize(SD.Count, 1).Value = Application.Transpose(SD.keys)
End Sub
 
Upvote 0
You can also get the same results using Power Query.

Book1
BCDEF
3Fruit NamesCountry of originFruit NamesCustom
4strawberriesSpainstrawberriesSpain UK
5applesFranceapplesFrance Italy
6pearsSpainpearsSpain Italy
7figsTurkeyfigsTurkey Spain
8cherriesUKcherriesUK Italy
9orangeItalyorangeItaly Spain
10plumsFranceplumsFrance UK
11grapesSpaingrapesSpain South Africa
12applesItaly
13pearsItaly
14figsSpain
15cherriesItaly
16orangeSpain
17plumsUK
18grapesSouth Africa
19strawberriesUK
Sheet1


Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"Fruit Names"}, {{"Count", each _, type table}}),
    Extract = Table.AddColumn(Group, "Custom", each Text.Combine(Table.Column([Count],"Country of origin")," "))
in
    Extract
 
Upvote 0
The 2 options above produce a little bit different results than in your OP. This version matches the output of your op.

Book1
EFG
3Fruit NamesCountry of originExtract
4strawberriesSpainSpain UK
5applesFranceFrance Italy
6pearsSpainSpain Italy
7figsTurkeyTurkey Spain
8cherriesUKUK Italy
9orangeItalyItaly Spain
10plumsFranceFrance UK
11grapesSpainSpain South Africa
12applesItalyFrance Italy
13pearsItalySpain Italy
14figsSpainTurkey Spain
15cherriesItalyUK Italy
16orangeSpainItaly Spain
17plumsUKFrance UK
18grapesSouth AfricaSpain South Africa
19strawberriesUKSpain UK
Sheet1


Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    Group = Table.Group(Source, {"Fruit Names"}, {{"Values", each _, type table}}),
    Extract = Table.TransformColumns(Group ,{"Values", each Text.Combine(Table.Column(_,"Country of origin")," ")}),
    Merge = Table.NestedJoin(Source,{"Fruit Names"},Extract,{"Fruit Names"},"Extract",JoinKind.LeftOuter),
    Combine = Table.TransformColumns(Merge,{"Extract", each Text.Combine(Table.Column(_,"Values")," ")})
in
    Combine
 
Upvote 0

Forum statistics

Threads
1,214,605
Messages
6,120,476
Members
448,967
Latest member
visheshkotha

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