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

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
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,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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