How rename worksheet CodeName at runtime?

Juska

New Member
Joined
Sep 3, 2017
Messages
6
Hi
I have a form with a combobox that contains 1,2,3,4
I need to create a work worksheet at Runtime and assign it the following properties.
Name = "Week" & the value of combobox
CodeName = "wks"& Worksheet name


I get a compile error on the CodeName ( Can't assign to read only property)
Here is my code:
Private Sub CreateWorksheet()
Dim wks As Excel.Worksheet
Set wks = Excel.Sheets.Add
With wks
.Name = "Week" & frmPrinters.cboWeek.Text
.CodeName = "wks" & .Name
End With
End Sub


Please assist
 

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
Try
Code:
Private Sub CreateWorksheet()

   Dim sht As String
   sht = "Week" & frmPrinters.cboWeek.Text
   
   Sheets.Add.Name = sht
   ThisWorkbook.VBProject.VBComponents(Sheets(sht).CodeName).Name = "Wks" & sht
End Sub
But this won't work if the VBA Project is locked. You'll also have to ensure that you have checked "Trust access to the VBA project object model" in macro settings
 
Upvote 0

Forum statistics

Threads
1,214,643
Messages
6,120,702
Members
448,980
Latest member
CarlosWin

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top