I would keep a very hidden sheet with the formula you used referencing each sheet.
When the Workbook_NewSheet
event fires a formula pointing to the new sheet is created:
- Create a sheet and give it the Code Name of
shtNames
.
- Give the sheet a tab name of
SheetNames
.
- In cell
A1
of shtNames
add a heading (I just used "Sheet List").
- In Properties for the sheet change Visible to 2 - xlSheetVeryHidden.
You can only do this if there at least one visible sheet left.
- Add this code to the
ThisWorkbook
module:
Private Sub Workbook_NewSheet(ByVal Sh As Object)
With shtNames
.Cells(.Rows.Count, 1).End(xlUp).Offset(1).Formula = _
"=RIGHT(CELL(""filename"",'" & Sh.Name & "'!$A$1), " & _
"LEN(CELL(""filename"",'" & Sh.Name & "'!$A$1))-" & _
"FIND(""]"",CELL(""filename"",'" & Sh.Name & "'!$A$1),1))"
End With
End Sub
Create a named range in the Name Manager:
- I called it
SheetList
.
- Use this formula:
=SheetNames!$A$2:INDEX(SheetNames!$A:$A,COUNTA(SheetNames!$A:$A))
You can then use SheetList
as the source for Data Validation lists and list controls.
Two potential problems I haven't looked at yet are rearranging the sheets and deleting the sheets.
so when someone changes a sheetname the macro keeps working
As @SNicolaou said though - use the sheet code name which the user can't change and your code will carry on working no matter the sheet tab name.