Functions/VBA for regrouping and merging data

VoidGenome

New Member
Joined
Sep 29, 2019
Messages
5
Could anyone please point me in right directions on how to do this with functions/VBA?
0000000.png


The input range is fixed (A3:B10), but the data in both columns are variables.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Hi & welcome to MrExcel.
What version of Xl are you using?
 
Upvote 0
Can be done with Power Query. Here is MCode

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Transposed Table" = Table.Transpose(Source),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    #"Merged Columns" = Table.CombineColumns(#"Promoted Headers",{"1A", "1A_1", "1A_2"},Combiner.CombineTextByDelimiter(", ", QuoteStyle.None),"1A.1"),
    #"Merged Columns1" = Table.CombineColumns(#"Merged Columns",{"2A", "2A_3"},Combiner.CombineTextByDelimiter(", ", QuoteStyle.None),"2A.1")
in
    #"Merged Columns1"
 
Upvote 0
With functions if you have the TEXTJOIN function in your Excel version
Both formulas copied down.


Excel 365
ABCDE
2ClassName
31AJohn1AJohn, Mary, Peter
41AMary2AThomas, Rosie
51APeterC3Luna
62AThomas
72ARosie
8C3Luna
9
10
Sheet2
Cell Formulas
RangeFormula
D3=IFERROR(INDEX($A$3:$A$10,MATCH(0,INDEX(COUNTIF($D$2:D2,$A$3:$A$10)+(A$3:A$10=""),0),0)),"")
E3{=IF(D3="","",TEXTJOIN(", ",1,IF(A$3:A$10=D3,B$3:B$10,"")))}
Press CTRL+SHIFT+ENTER to enter array formulas.


With VBA, try ..

Code:
Sub ClassNames()
  Dim d As Object
  Dim c As Range
  
  Set d = CreateObject("Scripting.Dictionary")
  For Each c In Range("A3", Range("A" & Rows.Count).End(xlUp))
    If d.exists(c.Value) Then
      d(c.Value) = d(c.Value) & ", " & c.Offset(, 1).Value
    Else
      d(c.Value) = c.Offset(, 1).Value
    End If
  Next c
  Range("D3:E3").Resize(d.Count).Value = Application.Transpose(Array(d.Keys, d.Items))
End Sub
 
Upvote 0
Can be done with Power Query. Here is MCode

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Transposed Table" = Table.Transpose(Source),
    #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars=true]),
    #"Merged Columns" = Table.CombineColumns(#"Promoted Headers",{"1A", "1A_1", "1A_2"},Combiner.CombineTextByDelimiter(", ", QuoteStyle.None),"1A.1"),
    #"Merged Columns1" = Table.CombineColumns(#"Merged Columns",{"2A", "2A_3"},Combiner.CombineTextByDelimiter(", ", QuoteStyle.None),"2A.1")
in
    #"Merged Columns1"
I'm not sure if Power Query is available on my office computer.....(I'm writing this excel to help me during work). I'll check it out tomorrow!
 
Upvote 0
Hi
What about
Code:
Sub TEST()    Dim a, i
        With CreateObject("scripting.dictionary")
            For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row - 2
                a = Range(Cells(3, 1), Cells(12, 2))
                If Not .exists(a(i, 1)) Then
                    .Add a(i, 1), a(i, 2)
                Else
                    .Item(a(i, 1)) = .Item(a(i, 1)) & "," & a(i, 2)
                End If
            Next
            Cells(3, 5).Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
        End With
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,759
Messages
6,126,730
Members
449,333
Latest member
Adiadidas

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