logo
search
VBA & Macro Problems

How to Combine Excel Rows with Same Family Data Using Macros

Huma Ashraf ChHuma Ashraf Ch Sep 29, 2026 869 views

Question details

The user needs to group individual family members under a single family record in Excel using a reusable macro to automate the formatting of exported data.

How to Combine Excel Rows with Same Family Data Using Macros
Product
Excel
Device & OS
not provided
Scenario
Organizing and restructuring exported worksheet data to group people by family ID.
Observed behavior
Exported data lists individual family members on separate rows with repeated family details, requiring grouping into a structured hierarchical layout.
Before you start

Before writing the VBA code, ensure you have enabled the Developer tab in your ribbon menu and converted your source data into an Excel Table (Ctrl + T) to handle dynamic row counts automatically.

Solution 1Recommended

Use a VBA Macro to Group Rows by Family ID

Create a custom VBA macro to loop through the exported data, detect repeated family IDs, and restructure the layout automatically.

This method is ideal for recurring data exports. By using a macro, you can automate the entire regrouping process instead of manually copying and pasting rows to combine families.

1
Open the VBA Editor

Press Alt + F11 to open the VBA Editor, click 'Insert' on the top menu, and select 'Module' to create a new blank script.

2
Write the Loop Logic

Write code to loop through the input rows of your source Table. Add a condition to check if the current row's family ID matches the previous row's ID.

3
Populate Family Details

When a new family ID is detected, program the macro to select the destination row and write the family information (e.g., ID, Family Name, Address) once.

4
Populate Individual Details

Instruct the macro to move to the next row and insert the individual's details (Name, Age, Phone Number). For additional people with the same family ID, continue moving to the subsequent row to populate their details without duplicating the family header.

Use a VBA Macro to Group Rows by Family ID
Save for Future Use: Save the macro in a Personal Macro Workbook (Personal.xlsb) so you can easily run it after every new data export, regardless of the active workbook.

Automate Data Grouping with Macros in WPS Spreadsheet

WPS Spreadsheet provides robust support for VBA and macros (in compatible versions), allowing you to automate repetitive tasks like grouping family rows seamlessly.

  1. 1. Open Your Data: Launch WPS Spreadsheet and open the workbook containing your exported family rows.
  2. 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click on the 'VBA Editor' icon.
  3. 3. Run the Macro: Paste your grouping script into a new Module and press F5 to instantly combine and format your family data.
Fully compatible with Microsoft Excel (.xlsx and .xlsm) macro formats.Supports standard VBA macro recording and execution for complex data sorting.Lightweight, feature-rich, and highly efficient for large data exports.
microsoft office alternative - wps office

Frequently Asked Questions

Can I group family rows without using a VBA macro?

Yes, you can use Excel's built-in PivotTable feature or Power Query to group and summarize data by Family ID. However, a macro provides more precise control if you need a very specific row-by-row layout.

Why do I get a security warning when running the macro?

Macros are disabled by default for security reasons. You must click 'Enable Content' in the yellow security warning bar at the top of your worksheet, or adjust your macro security settings in the Trust Center.

How do I make the macro available for all my future exports?

Save the macro in your Personal Macro Workbook. This creates a hidden workbook that loads automatically, ensuring the VBA code is available every time you open the application on your computer.