Excel: Group and combine row

Jessica Blue

New Member
Joined
Jun 13, 2024
Messages
4
Office Version
  1. 2021
Hi,

In excel, do we have a function or button that can transform the tables below?

Input table
Asset name 1 name 2 name 3 name 4
Toy. a.
Toy. B
Toy. C
Candy. D.
Candy. A
Play. C.




Output table
Asset name 1 name 2 name 3 name 4
Toy. a. B. C
Candy. D. A
Play c

Thanks
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
Pivot Tables are what you need to learn. Your friend Mr. Google will instruct you on how to build them.
 
Upvote 0
Here's a formula option.
Book1
ABCDEFG
1ToyaToyaBC
2ToyBCandyDA
3ToyCPlayC
4CandyD
5CandyA
6PlayC
Sheet2
Cell Formulas
RangeFormula
D1:D3D1=UNIQUE(A1:A6)
E1:G1,E3,E2:F2E1=TRANSPOSE(FILTER($B$1:$B$6,$A$1:$A$6=D1))
Dynamic array formulas.
 
Upvote 0
No, the same formula won't work when your data are in different columns. Since you don't have Office 365, the formula's a little challenging. Are you open to VBA?
 
Upvote 0
Yes. I open to VBA and asked my friend to build one. However, VBA can only accept process the data with around 16000row. And we got a overflow error since our data has 700k rows.
 
Upvote 0
Give this a try. I've made some comments where you might need to adjust the code.
VBA Code:
Sub GroupData()
    Dim ws As Worksheet
    Set ws = ActiveSheet
    Dim lastRow As Long
    Dim key As Variant
   
    Set ws = ThisWorkbook.Worksheets("Sheet2") 'Change sheet name to suit
    lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row 'Assumes data starts in A

    Dim data As Variant
    data = ws.Range("A2:D" & lastRow).Value 'Assumes data starts in row 2
    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")
    Dim i As Long
    For i = 1 To UBound(data, 1)
        key = data(i, 1)
        If Not dict.exists(key) Then
            dict.Add key, ""
        End If
        Dim j As Long
        For j = 2 To 4 ' 4 columns: Asset, Name1, Name2, Name3
            If data(i, j) <> "" Then
                If dict(key) = "" Then
                    dict(key) = data(i, j)
                Else
                    dict(key) = dict(key) & " " & data(i, j)
                End If
            End If
        Next j
    Next i

    Dim resultRow As Long
    resultRow = dict.Count
    Dim resultArray() As Variant
    ReDim resultArray(1 To resultRow, 1 To 4) ' 4 columns: Asset, Name1, Name2, Name3
    Dim rowIndex As Long
    rowIndex = 1

    For Each key In dict.Keys
        resultArray(rowIndex, 1) = key
        Dim values() As String
        values = Split(dict(key), " ")
        Dim colIndex As Long
        For colIndex = 0 To UBound(values)
            resultArray(rowIndex, colIndex + 2) = values(colIndex)
        Next colIndex
        rowIndex = rowIndex + 1
    Next key
   
    ' Output to F2
    ws.Range("F2").Resize(UBound(resultArray, 1), UBound(resultArray, 2)).Value = resultArray
End Sub
 
Last edited:
Upvote 0

Forum statistics

Threads
1,221,618
Messages
6,160,866
Members
451,674
Latest member
TJPsmt

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