For some reason the below code only works when run in the vbe, when run in the event handling code, or by macro list the codename remains sheet1.
Please can someone investigate ?
Sub changesheetcodename()
Dim ws,tsst as worksheet
For Each Ws In ActiveWorkbook.Worksheets
If Ws.Name <> "Instructions" then ws.delete
Next Ws
Worksheets.Add(After:=Worksheets(Worksheets.Count)).Name = "Sheet1"
set tsst = Sheets("Sheet1")
With tsst
.Parent.VBProject.VBComponents(.CodeName) _
.Properties("_CodeName") = "test"
End With
End Sub
Renaming sheets in Excel one can do from the taskbar below the worksheets by double-clicking on them. But in VBA, we use the Sheets or Worksheet property method to rename the sheet. The syntax to rename a sheet is as follows: Sheets(“Old Sheet Name”). Name = “New Sheet name.”
Sheet name code Excel formula Step 1: Type “CELL(“filename”,A1)”. The cell function is used to get the full filename and path. This function returns the filename of . xls workbook, including the sheet name.
A sheet name code is a friendly and meaningful name that you can assign to the sheets in your workbook. This is especially useful if you have more than one worksheet because the names can be different. Creating a sheet name in excel is very important to keep track of your data.
(2) In the Rename Options section, check the Replace original sheet name option; (3) In the New Worksheet Name section, check the From Specific range option, click the button to open the second Rename Multiple Worksheets dialog box, select the cells that you will rename by their values, and click the OK button.
Sub change_code_name()
Dim wbk As Object, sheet As Object
ActiveWorkbook.VBProject.Name = "VBAProject"
Set wbk = ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.CodeName)
wbk.Name = "wbk_code_name"
Set sheet = ActiveWorkbook.VBProject.VBComponents(ActiveWorkbook.Sheets(1).CodeName)
sheet.Name = "sheet_code_name"
End Sub
Also the access to the VBAProject
is required: see "Macro settings" and set "Trust access to the VBA object model". To programmatically change the settings, look here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With