How to Copy Rows with the Same ID to Separate Excel Worksheets
Question details
The user needs to automate the process of grouping rows by a unique ID and copying each specific group into its own corresponding worksheet, either within the same or another workbook.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Organizing a large dataset by splitting it into multiple sheets based on unique identifiers, such as employee names, department codes, or region IDs.
- Observed behavior
- Manually filtering, copying, and pasting grouped data into separate worksheets is highly time-consuming and prone to human error, requiring an automated solution.
Ensure your source data is formatted as a structured table with clear column headers, and verify that the column containing your unique IDs has no blank cells.
Use a VBA Macro to Automatically Split Data
A VBA macro is the most efficient way to automate filtering a dataset by unique IDs and distributing each group into its own newly created or existing worksheet.
VBA (Visual Basic for Applications) can iterate through your entire dataset, identify unique IDs, create corresponding tabs if they do not exist, and copy the relevant rows over in seconds.
Press Alt + F11 on your keyboard to open the Visual Basic for Applications Editor.
Click 'Insert' from the top menu and select 'Module'. A blank code window will appear.
Paste a VBA script designed to loop through your ID column, apply an AutoFilter for each unique value, and copy the visible cells to destination worksheets named after those IDs.
Press F5 or click the 'Run' button. The script will execute, automatically grouping your data and distributing the rows to the matching Excel worksheets.

Filter and Load Data using Power Query
Power Query allows you to create dynamic, refreshable connections to your source table, filtering by ID and loading groups into specific sheets without writing code.
Easily Automate Worksheets with WPS Spreadsheet
WPS Spreadsheet provides powerful data handling capabilities, including full VBA and macro support. You can seamlessly run scripts to automatically copy and distribute rows by unique IDs into separate tabs, making large-scale data organization effortless.
- 1. Open Your Data: Launch WPS Spreadsheet and open your dataset containing the unique IDs.
- 2. Access the Developer Tools: Navigate to the 'Developer' tab and click on 'Macros' or 'Visual Basic' to enter the VBA environment.
- 3. Run the Automation: Insert your VBA splitting script and click Run to instantly copy grouped rows into separate, neatly organized worksheets.

Frequently Asked Questions
Can I copy rows to a completely different workbook instead of just a new worksheet?
Yes, you can modify the VBA script to specify a target workbook object. You will need to ensure the destination workbook is either already open, or include a file path in your code so the macro can open the external workbook automatically before pasting.
Will the destination worksheets update automatically when the source data changes?
If you use a VBA macro, the split data is static; you will need to re-run the macro to update the sheets. If you use the Power Query method, you can simply go to Data > Refresh All, and the destination sheets will instantly reflect the updated source data.
What happens if a worksheet for a specific ID doesn't exist yet?
A well-written VBA splitting macro typically includes a check to see if a sheet with the ID's name exists. If it does not, the macro automatically creates a new worksheet, renames it to the ID, and then copies the corresponding rows into it.
How do I ensure the column headers are copied to every new sheet?
When writing or copying a VBA macro for this task, ensure the script is set to copy the header row (usually Row 1) and paste it into the destination sheet before pasting the filtered data. Power Query does this automatically when loading tables to new sheets.




