Data Import & Export

Merge Excel Rows with the Same Client ID into One Row

John WilsonJohn Wilson Nov 25, 2026 869 views

Question details

Combine activity records with the same Client ID into one row, keeping each value in a separate cell.

Merge Excel Rows with the Same Client ID into One Row
Product
Microsoft Excel for Mac 2011
Device & OS
Mac
Scenario
A worksheet contains multiple activity rows for each client, and the user wants one output row per client.
Observed behavior
Each client should have one row, with the activity values from their original rows arranged in successive columns.
Before you start

Make sure Client IDs are in column A, headers are in row 1, and activity fields occupy contiguous columns starting in column B. Save a copy of the workbook as a macro-enabled .xlsm file and test the macro on a small set of records first.

Solution 1Recommended

Use a VBA macro to group activity rows by Client ID

Use this method to create a separate output sheet with one row per Client ID and a block of activity columns for each source row.

The macro reads the active worksheet in the workbook containing the macro. It skips rows with blank or error-valued Client IDs and groups IDs as text without distinguishing letter case.

Values are copied, not formulas or formatting. Check IDs that rely on number formatting for leading zeros, and ensure the output has enough columns for each client's activity records.

1
Prepare the source sheet

Open the .xlsm workbook and select the worksheet containing the data. Confirm that column A has Client IDs and that row 1 has a header for each activity column. Rename or remove any existing sheet named Merged Output.

2
Add the macro

Open Tools > Macro > Visual Basic Editor, choose Insert > Module, and paste this code into the module: Sub MergeClientRows() Dim src As Worksheet, dst As Worksheet, existing As Worksheet Dim groups As Collection Dim lastRow As Long, lastCol As Long, r As Long, c As Long Dim outRow As Long, groupRow As Long, startCol As Long Dim nextCol() As Long, idValue As Variant, key As String Set src = ActiveSheet If Not src.Parent Is ThisWorkbook Then MsgBox "Select the source sheet in the workbook containing this macro." Exit Sub End If On Error Resume Next Set existing = ThisWorkbook.Worksheets("Merged Output") On Error GoTo 0 If Not existing Is Nothing Then MsgBox "Rename or remove the existing Merged Output sheet first." Exit Sub End If lastRow = src.Cells(src.Rows.Count, 1).End(xlUp).Row lastCol = src.Cells(1, src.Columns.Count).End(xlToLeft).Column If lastRow < 2 Or lastCol < 2 Then MsgBox "Add data rows and at least one activity column." Exit Sub End If Set dst = ThisWorkbook.Worksheets.Add(After:=src) dst.Name = "Merged Output" dst.Cells(1, 1).Value = src.Cells(1, 1).Value Set groups = New Collection outRow = 1 For r = 2 To lastRow idValue = src.Cells(r, 1).Value2 If Not IsError(idValue) Then If Len(Trim(CStr(idValue))) > 0 Then key = "id:" & CStr(idValue) groupRow = 0 On Error Resume Next groupRow = groups(key) On Error GoTo 0 If groupRow = 0 Then outRow = outRow + 1 groupRow = outRow groups.Add groupRow, key ReDim Preserve nextCol(1 To outRow) nextCol(groupRow) = 2 dst.Cells(groupRow, 1).Value2 = idValue End If startCol = nextCol(groupRow) If startCol + lastCol - 2 > dst.Columns.Count Then MsgBox "Not enough output columns. The output sheet is incomplete." Exit Sub End If For c = 2 To lastCol dst.Cells(1, startCol + c - 2).Value = _ "Activity " & ((startCol - 2) \ (lastCol - 1) + 1) & " - " & src.Cells(1, c).Value dst.Cells(groupRow, startCol + c - 2).Value2 = src.Cells(r, c).Value2 Next c nextCol(groupRow) = startCol + lastCol - 1 End If End If Next r MsgBox "Finished. Check the Merged Output sheet." End Sub

3
Run and verify the result

Return to the source worksheet, choose Tools > Macro > Macros, select MergeClientRows, and click Run. Open Merged Output and check several clients against their original rows, including any blank activity cells.

Use a VBA macro to group activity rows by Client ID
Output column limit: Each source row adds another block of activity columns. If a client has too many records to fit across the worksheet, the macro stops and reports that the output is incomplete.
Work with client records in WPS

Combine a Few Client Activity Rows in WPS Spreadsheets

For a small dataset, use WPS Spreadsheets to filter by Client ID and copy each activity row into successive columns on a separate sheet.

  1. 1. Open the workbook: Open the Excel workbook in WPS Spreadsheets and select the sheet containing the Client ID and activity columns.
  2. 2. Filter for one client: Select the header row, choose Data > Filter, and use the Client ID filter to show one client's records.
  3. 3. Create an output row: Add a new worksheet with the plus button beside the sheet tabs. Enter the Client ID in column A and add activity headers across row 1.
  4. 4. Copy activity blocks: On the source sheet, select and copy the activity cells from one visible record. Paste them into the output row starting in column B, then copy each remaining visible record into the next empty block of columns.
  5. 5. Repeat and save: Change the Client ID filter for each remaining client, repeat the copying process on a new output row, and use File > Save As to save the result.
Place each activity value in its own cell on a client rowFilter records by Client ID before copyingOpen and save Microsoft Excel formats such as XLSXUse a familiar spreadsheet interface
microsoft office alternative - wps office

Frequently Asked Questions

Will the macro combine activity values into a single cell?

No. It writes each source activity value to its own cell, with each source row occupying a successive block of columns.

What happens to rows with no Client ID?

The macro skips rows whose column A ID is blank or contains an Excel error. Check and correct those IDs on the source sheet before running it if they need to be included.

Does the macro preserve formulas and cell formatting?

No. It copies cell values into Merged Output, not formulas or formatting. Check IDs displayed with formatting-based leading zeros before using the result.

Why won't the macro create the output sheet?

It stops if a worksheet named Merged Output already exists. Rename or remove that sheet, select the source worksheet, and run MergeClientRows again from Tools > Macro > Macros.