How to Set an Excel Worksheet CodeName During VBA Creation
Question details
The user needs to programmatically set the VBA reference name (CodeName) of a new worksheet at the time of creation, rather than renaming the object manually.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Creating a new worksheet via VBA and assigning it a specific CodeName for easier, more robust referencing within the macro code.
- Observed behavior
- The user is forced to manually rename the worksheet object in the Visual Basic Editor because standard creation methods only set the worksheet's display tab name.
Before running the macro, ensure that you have enabled 'Trust access to the VBA project object model' in your Trust Center settings, as this permission is strictly required to modify VBComponents.
Set Worksheet CodeName via VBProject.VBComponents
Use the VBProject object model to programmatically rename the worksheet's CodeName immediately after generating the new sheet.
When a worksheet is created, Excel assigns a default CodeName (such as Sheet1, Sheet2). To overwrite this default name programmatically, you must access the VBComponents collection through the workbook's VBProject.
Because this involves manipulating the macro project itself, Excel requires explicit user permission to allow macros to access the VBA project object model.
In Excel, navigate to File > Options > Trust Center. Click on 'Trust Center Settings', select 'Macro Settings' from the left pane, check the box for 'Trust access to the VBA project object model', and click OK.
Press ALT + F11 to open the Visual Basic Editor. Right-click on your workbook in the Project Explorer pane and select Insert > Module.
Declare your Workbook and Worksheet variables. Create the new sheet using the standard command: 'Set sht = wrk.Sheets.Add'.
Add the command 'wrk.VBProject.VBComponents(sht.CodeName).Name = "YourNewCodeName"' immediately after the sheet creation. Replace 'YourNewCodeName' with your desired reference string.

Write and Execute VBA Macros in WPS Spreadsheets
WPS Office provides robust support for VBA and macros. You can easily automate tasks, manage worksheets, and write custom scripts just like you do in Microsoft Excel, with full compatibility for existing .xlsm and .xlsb files.
- 1. Install WPS Office: Download and install WPS Office, ensuring that the optional VBA module is enabled during setup.
- 2. Open your Macro Workbook: Launch WPS Spreadsheets and open your macro-enabled workbook (.xlsm) or create a new document.
- 3. Access the VBA Editor: Navigate to the Tools tab on the top ribbon and click on the 'VBA Editor' icon to open the development environment.
- 4. Write your Script: Insert a new module and write or paste your VBProject code to manipulate worksheet CodeNames just as you would normally.

Frequently Asked Questions
Why am I getting a run-time error when trying to change the CodeName?
This usually happens if 'Trust access to the VBA project object model' is not enabled in your Macro Security settings. Excel blocks programmatic access to the VBA project by default to prevent malicious scripts from modifying your code.
What is the difference between an Excel Worksheet Name and a CodeName?
The Worksheet Name (or Tab Name) is what the user sees on the sheet tab at the bottom of the window, changed via 'sht.Name'. The CodeName is the internal object name used in the VBA Editor to reference the sheet directly, which prevents your macros from breaking if a user renames the tab.
Can I change the CodeName of an Excel worksheet without writing VBA?
Yes, you can manually change a worksheet's CodeName by opening the Visual Basic Editor (ALT + F11), selecting the specific sheet in the Project Explorer, and editing the '(Name)' property in the Properties Window (press F4 if it is not visible).
Why does my CodeName change fail when I include spaces?
VBA object names, including worksheet CodeNames, must adhere to strict variable naming conventions. They cannot contain spaces, standard punctuation, or start with a number. Use camelCase or underscores (e.g., 'My_Sheet' or 'MySheet') instead.




