How to Combine Excel Rows with Same Family Data Using Macros
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.

- 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 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.
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.
Press Alt + F11 to open the VBA Editor, click 'Insert' on the top menu, and select 'Module' to create a new blank script.
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.
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.
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.

Alternative: Use Named Ranges for Manual Setup
If you prefer not to use VBA, you can define specific Named Ranges for each family to organize the data.
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. Open Your Data: Launch WPS Spreadsheet and open the workbook containing your exported family rows.
- 2. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click on the 'VBA Editor' icon.
- 3. Run the Macro: Paste your grouping script into a new Module and press F5 to instantly combine and format your family data.

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.




