Would really appreciate any help that can be provided! I feel like this question is basic and should be easy to figure out but I am struggling. I have an Excel 2016 workbook that, if a set-up error is made, code will need to run to reset the names of the worksheets back to a "standard". The worksheet location (Sheets(1)) can't be used as the worksheet position may change and the visible worksheet name (Sheets("Name")) can't be used as it won't be constant. This leaves using the codename (Sheet1) as the appropriate option. I set-up an array and simple code to rename the sheets but it won't run.
'WSName to define array of sheets by codename
'NewName to create reference to cell which contains new worksheet name
Dim WSName as Variant
Dim NewName as Variant
WSName = Array ("Sheet4","Sheet15","Sheet17","Sheet18")
NewName = Array("D15","D16","D17","D18")
'counter to cycle through arrays and rename the 4 worksheets
For Ncounter = 0 To 3
Sheets(WSName(Ncounter)).Name=Range(NewName(Ncounter)).Value
Next Ncounter
I can replace the WSName array with the user added sheet names and the code works. I've also tried removing "Sheets" from the code as I'm effectively saying SheetsSheetX but still nothing.
I think I am missing the proper syntax for referencing a sheet by codename. Any help or suggestions would be appreciated and I apologize for what, I think, is a newbie and just don't get it question.
Update: Based upon Tim's answer, I did this:
'WSName to define array of sheets by codename
'NewName to create reference to cell which contains new worksheet name
Dim WSName, NewName as Variant
WSName = Array (Sheet4, Sheet15, Sheet17, Sheet18)
NewName = Array("D15","D16","D17","D18")
'counter to cycle through arrays and rename the 4 worksheets
For Ncounter = 0 To 3
WSName(Ncounter).Name=Range(NewName(Ncounter)).Value
Next Ncounter
This code cycles through 4 sheets out of 20 and renames those 4 to the values listed on the active worksheet. For the Dim, I'm sure that long instead of variant will work fine (better actually), just need to research that one a little more before switching out so I'm sure I'm comfortable with the difference between the 2. Thanks again to all that responded...... Angelic