Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Sheet code name

Tags:

excel

vba

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
like image 689
Krishn Avatar asked Jul 17 '15 09:07

Krishn


People also ask

How do I rename a sheet name in VBA?

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.”

Where is the sheet name code in Excel?

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.

What is a sheet name code?

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.

How do I rename a sheet based on cell value?

(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.


1 Answers

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.

like image 97
Aleksey F. Avatar answered Nov 15 '22 09:11

Aleksey F.