VBA & Macro Problems

How to Copy Rows with the Same ID to Separate Excel Worksheets

Emma BrownEmma Brown Nov 11, 2026 868 views

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.

How to Copy Rows with the Same ID to Separate Excel Worksheets
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 on your keyboard to open the Visual Basic for Applications Editor.

2
Insert a New Module

Click 'Insert' from the top menu and select 'Module'. A blank code window will appear.

3
Paste the Splitting Code

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.

4
Run the Macro

Press F5 or click the 'Run' button. The script will execute, automatically grouping your data and distributing the rows to the matching Excel worksheets.

Use a VBA Macro to Automatically Split Data
Automation Tip: You can assign this macro to a button on your main worksheet so you can run the splitting process with a single click whenever your source data changes.
Organize Data Effectively

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. 1. Open Your Data: Launch WPS Spreadsheet and open your dataset containing the unique IDs.
  2. 2. Access the Developer Tools: Navigate to the 'Developer' tab and click on 'Macros' or 'Visual Basic' to enter the VBA environment.
  3. 3. Run the Automation: Insert your VBA splitting script and click Run to instantly copy grouped rows into separate, neatly organized worksheets.
Fully compatible with Microsoft Excel (.xlsx and .xlsm) formatsBuilt-in support for VBA macros to automate repetitive tasksAdvanced filtering and PivotTable tools for complex data analysisFree, lightweight, and user-friendly interface
microsoft office alternative - wps office

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.