I am trying to sum cell Y116 across all valid worksheets in my excel workbook. To help define the valid worksheets, I wrote a VBA function, SHEETNAME(number), that returns the name of the worksheet at the given worksheet index (number). I did this because the names and number of valid worksheets will never be constant, however the valid range will always start at the 3rd worksheet (i.e. SHEETNAME(3)) and will always end at the third from last worksheet (i.e. SHEETNAME(SHEETS()-2)) in my workbook.
I feel like this should be relatively straightforward with both SUM() and INDIRECT(), but I keep getting reference errors (#REF!).
I can get the string formatted how I want it with
="'"&SHEETNAME(3)&":"&SHEETNAME(SHEETS()-2)&"'!Y116"
but I get a reference error when I try to put it all together:
=SUM(INDIRECT("'"&SHEETNAME(3)&":"&SHEETNAME(SHEETS()-2)&"'!Y116"))
I know cell Y116 is a valid reference in all of my worksheets because I can hardcode the formula with the actual names of the worksheets instead of the index and I get the answer I am looking for. Any advice?
Here is the SHEETNAME() function:
Function SHEETNAME(number As Long) As String
SHEETNAME = Sheets(number).Name
End Function
=SUM('Sheet1:Sheet3'!Y116)
will do just that. OP, please share the UDFSHEETNAME()
so we can test. – Scott Craner