Skip to main content

Kutools for Office — One Suite. Five Tools. Get More Done.

3 methods to concatenate rows into one cell based on group in Excel

Author Sun Last modified

Combining multiple rows of data into a single cell based on a group is a common requirement when working with structured datasets in Excel. For example, you may want to merge all names associated with the same class into one cell. This article provides step-by-step instructions using various methods and optimizations for different levels of expertise and use cases.

A screenshot of two columns with classes and student names, and concatenated student names based on classes


Group and concatenate with formulas and Filter function

You can use Excel's formulas along with the "Filter" function to concatenate rows based on one column. Please do as follows.

Note: Ensure your data is sorted by the grouping column, such as "Class" in this case.
  1. In a new column, enter the following formula to concatenate the names and press "Enter". Drag down to apply it through the column range.
    =IF(A2=A1,C1&", "&B2,B2)
  2. In a new column, enter the following formula, press "Enter" and drag down the formula.
    =IF(A2<>A3,"Last","")
  3. Select all the data range including the helper columns. Select "Data" > "Filter" to add Filter icons to the data.
    A screenshot of the Filter button in Excel ribbon
  4. Click the "Filter icon" in last helper column, only tick the "Last" checkbox, and click "OK".
    A screenshot showing only filtered last rows in each group in Excel

Now the result is shown as below, you can remove the last helper column if you do not need it any more

A screenshot of final grouped and concatenated results in Excel


Group and concatenate with Kutools for Excel easily

"Kutools for Excel" offers a simple and efficient solution - "Advanced Combine Rows", allowing you to merge grouped rows into a single cell with just a few clicks. It automatically adds custom separators such as commas, spaces, or line breaks to meet diverse data presentation needs.

Kutools for Excel - Packed with over 300 essential tools for Excel. Enjoy permanently free AI features! Download now!

  1. Highlight the cells that contain you data. Navigate to "Kutools" > "Merge & Split" > "Advanced Combine Rows".
  2. In the "Advanced Combine Rows" window, please configure as follow:
    1. Click the column which you want to combine rows based on, and set as "primary key".
    2. For the column you want to concatenate, such as the "Names" column in this case, specify a delimiter in the "Combine" section under the "Operation" field for this column.
    3. Click "OK" button.

Result: Kutools will automatically merge data based on the group into a single cell with specific delimiter.

Note: Before applying the utility, it's recommended to create a copy of the original data.

Kutools for Excel - Supercharge Excel with over 300 essential tools. Enjoy permanently free AI features! Get It Now


Group and concatenate with VBA code

You can also use VBA to concatenate rows based on a group dynamically.

  1. Press "Alt" + "F11" keys to enable the "Microsoft Visual Basic for Applications" window.
  2. In the code editor, click "Tools" > "References" to enable "References" dialog, check "Microsoft Scripting Runtime" and save the changes.
  3. Go to "Insert" > "Module" and paste the following VBA code into the Module window.
    VBA: Concatenate rows into one cell based on group
    Sub ConcatenateCellsIfSameValues()
    'UpdatebyExtendoffice20180201
        Dim I As Long
        Dim J As Long
        Dim xRg As Range
        Dim xRgKey As Range
        Dim xRgVal As Range
        Dim xStr As String
        Dim xDic As New Dictionary
        On Error Resume Next
        Set xRg = Application.InputBox("Select data range", "KuTools for Excel", Selection.Address, , , , , 8)
        If xRg Is Nothing Then Exit Sub
        Set xRgKey = Application.InputBox("Select key column", "KuTools for Excel", xRg.Columns(1).Address, , , , , 8)
        If xRgKey Is Nothing Then
            MsgBox "Key column cannot be empty", vbInformation, "KuTools for Excel"
        End If
        Set xRgVal = xRg(1).Offset(, 1).Resize(xRg.Rows.Count, xRg.Columns.Count - 1)
        For I = 1 To xRgKey.Count
            If I > xRgKey.Count Then Exit For
            xStr = ""
            For J = 1 To xRgVal.Columns.Count
                xStr = xStr & " " & xRgVal(I, J)
            Next
            If xDic.Exists(xRgKey(I).Text) Then
                xDic(xRgKey(I).Text) = xDic(xRgKey(I).Text) & xStr
                xRgKey(I).EntireRow.Delete
                I = I - 1
           Else
                xDic.Add xRgKey(I).Text, xStr
            End If
        Next
        For I = 1 To xRgVal.Count
            xRgVal(I).Value = xDic(xRgKey(I).Text)
        Next
    End Sub
    A screenshot showing pasted VBA code in module window
  4. Press the "F5" key to run the script. and follow any prompts to select your data range and specify key columns.
    A screenshot of the VBA input box prompting for range selection in Excel A screenshot of the VBA input box prompting for key column selection

The result is shown as below:
A screenshot showing the concatenated results after running the VBA script

When working with data in Excel, grouping and merging cells is a common yet complex task. Depending on your needs and tools, you can choose from several methods to achieve this goal. Select the most appropriate tool and method based on your specific needs and preferences to make your Excel work more efficient and convenient. If you're interested in exploring more Excel tips and tricks, our website offers thousands of tutorials to help you master Excel.

Demo: Group and concatenate with Advanced Combine Rows of Kutools for Excel

 
Kutools for Excel: Over 300 handy tools at your fingertips! Enjoy permanently free AI features! Download Now!

Best Office Productivity Tools

šŸ¤– Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions…
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more
Use Kutools in your preferred language – supports English, Spanish, German, French, Chinese, and 40+ others!

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!

All Kutools add-ins. One installer

Kutools for Office suite bundles add-ins for Excel, Word, Outlook & PowerPoint plus Office Tab Pro, which is ideal for teams working across Office apps.

Excel Word Outlook Tabs PowerPoint
  • All-in-one suite — Excel, Word, Outlook & PowerPoint add-ins + Office Tab Pro
  • One installer, one license — set up in minutes (MSI-ready)
  • Works better together — streamlined productivity across Office apps
  • 30-day full-featured trial — no registration, no credit card
  • Best value — save vs buying individual add-in