How to Rename Worksheets When Splitting a Workbook with Excel VBA
Question details
The user needs a VBA macro solution to split an Excel workbook into multiple workbooks, including a specific persistent sheet, and rename the copied target worksheet to a predefined name before saving.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- Using a VBA Splitbook procedure to create new workbooks from existing worksheets, while managing specific sheet inclusions and renaming.
- Observed behavior
- The user needs to rename the newly separated worksheet to 'algemeen' in the new workbook, while preserving another included sheet 'Blad41'.
Before modifying your VBA macros, duplicate your original workbook as an .xlsm or .xlsb file to ensure your existing code and underlying data are protected against accidental logic errors.
Modify the VBA Splitbook Macro to Rename the Copied Worksheet
Update your existing Splitbook VBA script to select the newly copied worksheet in the active workbook and change its name property right before saving the new file.
When splitting a workbook using VBA, you first copy the target worksheet along with any persistent sheets (like 'Blad41') to a new workbook. Since the newly generated workbook automatically becomes the 'ActiveWorkbook', you can directly target the copied sheet by its original name and assign a new name string before executing the SaveAs command.
Press Alt + F11 in Excel to open the Visual Basic for Applications (VBA) editor and locate your specific Splitbook module.
Find the section of your code where the dynamic target worksheet (xWs) and the constant worksheet ('Blad41') are copied to a new workbook.
Immediately after the copy command, insert the following With block to handle the renaming and saving: With ActiveWorkbook .Worksheets(xWs.Name).Name = "algemeen" .SaveAs Filename:=xPath & "\\GW " & xWs.Name & ".xlsx" .Close False End With.
Ensure your macro loop still contains an If xWs.Name <> "Blad41" Then condition to prevent the macro from trying to iterate and split your persistent template sheet.
Save your code, close the VBA editor, and run the macro. Verify that each new directory file contains both the 'Blad41' sheet and the newly renamed 'algemeen' sheet.

Execute and Edit Splitbook Macros with WPS Spreadsheets
WPS Spreadsheets provides robust support for VBA macros, allowing you to run, edit, and troubleshoot your Excel scripts seamlessly without changing your existing coding workflow.
- 1. Enable Developer Tools in WPS: Open WPS Spreadsheets, navigate to the 'Developer' tab on the top ribbon, and click 'Macros' or 'Visual Basic' to access the VBA environment.
- 2. Import Your Workbook: Open your existing .xlsm Excel file containing the Splitbook macro directly in WPS Office.
- 3. Edit and Run the VBA Script: Paste or modify your worksheet renaming code within the WPS Visual Basic editor, then hit 'Run' to seamlessly split and rename your worksheets.

Frequently Asked Questions
Why do I get a 'Subscript out of range' error when renaming the copied worksheet?
This error typically occurs if the worksheet name referenced in .Worksheets(xWs.Name) doesn't perfectly match the active sheet, or if the new workbook hasn't become the ActiveWorkbook. Ensure the renaming code block is placed immediately after the sheets are copied.
Can I use VBA to rename a worksheet dynamically based on a cell value instead of a fixed string?
Yes. Instead of using ="algemeen", you can assign the name dynamically by referencing a specific cell, such as .Name = xWs.Range("A1").Value. Just ensure the cell contains a valid worksheet name without forbidden characters like brackets, slashes, or asterisks.
How do I ensure the excluded worksheet 'Blad41' is always copied along with the dynamic worksheet?
You can copy multiple sheets simultaneously in VBA by passing an array of sheet names to the Copy method. For example: Sheets(Array(xWs.Name, "Blad41")).Copy. This places both sheets into a newly created active workbook automatically.




