Name vs. Codename
A worksheet also has a
.codeName
property (as also have other objects such as a
chart
).
These values are equal when the object is created but may be changed independently from each other.
Apparently, the idea of code names is to use it in
VBA like so (assuming that the
codename
property value of a sheet is
figures
.
figures.cells(3, 2) = "Year 2019"
sheet1.name = "bar" ' Changes name of tab
sheet1.name = "baz" ' Change it again
? worksheets("bar").codeName ' Evaluates to "sheet1"
'
' Change code name of sheet is not possible at run time, so the
' following line throws Run-time error 32183: Application-defined or object-defined error.
'
activeWorkbook.vbProject.VBComponents("Sheet1").name = "NewCodeNameForTheSheet"
'
' Use new code name:
'
? newCodeNameForTheSheet.name ' Evaluates to "bar"
An example how the value of
.codeName
might be changed after creating a worksheet programmatically is found
here.