How to Use VBA to Split Excel Data Into Separate Worksheets by Column Value
Question details
The user needs a VBA macro to dynamically split a dataset into separate worksheets based on changing values in a specific column, avoiding issues with fixed row intervals.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Splitting a large converted Excel dataset into multiple tabs, organized by state names in Column A.
- Observed behavior
- The current macro uses fixed groups of 156 rows, causing row offsets to drift. It does not accurately split the data dynamically when the state name changes.
Ensure your data is sorted by the target column (e.g., Column A for states) so that identical values are grouped together, and save a backup copy of your workbook before running new macros.
Modify the VBA Macro for Dynamic Column Value Splitting
Update the macro to dynamically identify the start and end rows for each group based on column values instead of relying on a fixed row count.
Using a fixed row count (like 156 rows) often leads to drift if the data varies. A robust VBA script loops through the column, detects when the value changes, and copies the corresponding dynamic range into a newly named worksheet.
This method also requires a reliable function to determine the exact last row of the dataset and a safeguard to sanitize sheet names.
Select your entire dataset and sort it based on Column A (the state column) so that all rows belonging to the same state are grouped consecutively.
Press ALT + F11 to open the Visual Basic for Applications editor, then insert a new Module by clicking Insert > Module.
Create a VBA loop that reads down Column A. When Cells(r, 1).Value differs from Cells(r + 1, 1).Value, configure the script to mark the range from the group's start row down to row 'r', and copy it.
Use Worksheets.Add to generate a new sheet for each detected group. Add a helper function to replace invalid worksheet characters (like *, :, \, ?) with underscores before applying the column value as the sheet name.
Incorporate a dependable LastRow finding function (e.g., LastRow = Cells(Rows.Count, "A").End(xlUp).Row) to ensure the macro processes exactly to the end of the data without compile errors.

Easily Manage and Automate Data with WPS Spreadsheet
WPS Spreadsheet fully supports advanced VBA macros, enabling you to automate data splitting, reporting, and formatting seamlessly. It handles large datasets efficiently and features a built-in PDF conversion tool.
- 1. Open your file in WPS Spreadsheet: Launch WPS Office and open your .xlsm or .xlsx file containing the dataset.
- 2. Enable Macros: Navigate to the 'Developer' tab and click 'Macros' to ensure your VBA scripts are enabled.
- 3. Run the Split Macro: Select your dynamically adjusted split macro from the list and click 'Run' to divide your data into worksheets.

Frequently Asked Questions
Why does my VBA macro return an error on PDF-converted data?
Data converted from PDFs often contains hidden formatting, trailing spaces, or unrecognized characters. This can cause VBA functions (like finding the last row) to fail. Cleaning the data using the TRIM function or converting the PDF using a more accurate tool usually fixes this.
How do I deal with invalid characters in worksheet names using VBA?
Excel prohibits certain characters in sheet names (\, /, ?, *, [, ]). You can create a helper function in VBA using the Replace function to strip or swap these characters with underscores before applying the name to the newly created worksheet.
Can I split data into separate workbooks instead of worksheets?
Yes. Instead of using Worksheets.Add, your VBA script can use Workbooks.Add. After copying the designated range into the new workbook's sheet, use ActiveWorkbook.SaveAs to save it as a completely separate file.




