How to Preserve Formatting and Column Widths When Splitting Excel Data with VBA
Question details
The user needs to retain original header formats and exact column widths when utilizing a VBA macro to split grouped data into new worksheets or workbooks.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Running a VBA macro to split a large dataset into multiple separate sheets or files.
- Observed behavior
- The macro currently copies only the cell values, resulting in new worksheets that lose the original header row's formatting and column width settings.
Ensure your Excel workbook is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled Developer tools to access and edit your Visual Basic for Applications (VBA) code safely.
Use VBA PasteSpecial to Keep Column Widths and Header Formatting
Modify your existing VBA macro to use the Copy method with a specified Destination for headers, followed by PasteSpecial with xlPasteColumnWidths to retain the exact layout.
Directly assigning values (e.g., .Value = .Value) bypasses the clipboard and only transfers the raw data, dropping all visual styling. To ensure headers and columns look identical to the source, you must explicitly code copy and paste operations for both the format and the column width.
Locate the line in your macro that copies the header row. Replace the value-only assignment with: .Range(.Cells(1, 1), .Cells(1, LastCol)).Copy Destination:=ws.Cells(1, 1)
Add the following code right below your newly added copy command to apply the original widths to the new worksheet: .Range(.Cells(1, 1), .Cells(1, LastCol)).Copy followed by ws.Cells(1, 1).PasteSpecial Paste:=xlPasteColumnWidths
If you need to transfer other styles like number formats or cell colors specifically without overriding values, you can use additional PasteSpecial lines using Paste:=xlPasteFormats.
Press F5 in the VBA editor or trigger the macro from your workbook to confirm the new worksheets successfully retain both the header design and original column spacing.

Easily Run Macros and Handle Large Datasets in WPS Spreadsheet
WPS Spreadsheet provides excellent compatibility with VBA macros. You can run your existing split-data scripts, retain all formatting, and enjoy a lightweight, highly efficient spreadsheet experience without rewriting your code.
- 1. Open your Macro-enabled workbook: Launch WPS Spreadsheet and open your .xlsm file containing the data and VBA scripts.
- 2. Access the Developer Tab: Navigate to the 'Developer' tab on the top ribbon and click 'Macros' or 'Visual Basic' to view your existing scripts.
- 3. Execute the split macro: Run your modified VBA macro. WPS Spreadsheet will smoothly process the code, split the data, and preserve your original formatting and column widths as instructed.

Frequently Asked Questions
Why does assigning values directly in VBA destroy my cell formatting?
Assigning values via '.Value = .Value' is highly efficient for raw data transfer but completely bypasses the clipboard. It does not carry over cell colors, borders, font styles, or dimensions. To keep formatting, you must use the '.Copy' method.
Can I copy row heights the same way I copy column widths in VBA?
No, there isn't a direct PasteSpecial equivalent (like xlPasteColumnWidths) for row heights. To preserve row heights, you typically need to loop through the source rows and set the RowHeight property of the destination rows to match.
How do I copy both formats and column widths in a single VBA command?
You cannot execute both in a single PasteSpecial command. You must chain the operations by copying the range once and then calling PasteSpecial twice on the destination: once for xlPasteFormats and again for xlPasteColumnWidths.




