I've code which, when I press a button, will add a new sheet to a workbook and change the codename of the sheet to make it easier to refer to later in my code.
Dim wbk As Workbook
Dim wks As Worksheet
Set wbk = ThisWorkbook
wbk.Sheets.Add.Name = "Admin - Save Log"
Set wks = wbk.Worksheets("Admin - Save Log")
wks.Parent.VBProject.VBComponents(wks.CodeName).Name = "wksAdminSaveLog"
This does work - HOWEVER - only when I have the "Developer" window open or have previously had it open.
If I click the button when I first open the Excel document (having not opened the "Developer" window) it adds the sheet, however it comes up with the following error and does not change the codename of the sheet:
Run-time error '9': Subscript out of range
It is only when I press Debug and then run the code after the "Developer" window is open again that it adds the codename.
Is there any way around this so that the user does not have to have the developer window open in order for it to run correctly?