Reference an Excel Worksheet by Position Using a VBA Function
Question details
Reference a cell on an Excel worksheet by the worksheet's current position rather than its name.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A formula needs to return a cell from whichever worksheet occupies a specified position in the workbook.
- Observed behavior
- INDIRECT cannot directly use a worksheet index number to identify a worksheet.
Check that the workbook allows VBA macros and that you can save it as a macro-enabled workbook. Count worksheet positions from left to right, including hidden worksheets; chart sheets are not included in the Worksheets collection.
Create a VBA function that references a worksheet by index
Use a custom function when the formula must follow the worksheet currently at a given position.
The function uses the workbook containing the formula and returns the requested cell from its Worksheets collection. Application.Volatile makes the function recalculate whenever Excel recalculates.
In Excel, select Developer > Visual Basic, then select Insert > Module to add a standard module.
Paste this code into the module: Function ByIndex(index As Long, cellref As String) As Range Application.Volatile Set ByIndex = Application.ThisCell.Worksheet.Parent.Worksheets(index).Range(cellref) End Function
Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm). If prompted when reopening the file, enable content only if you trust the workbook.
In a worksheet cell, enter =ByIndex(3,"A1") to return A1 from the third worksheet. Replace 3 and "A1" with the position and cell address you need.
If you move worksheets and the result does not update, use Formulas > Calculate Now to recalculate the workbook.

Work with Excel spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and Microsoft Office format compatibility, making it easy to move and edit everyday workbooks. VBA macro support can vary by version and platform, so verify compatibility before relying on the custom function.

Frequently Asked Questions
Can INDIRECT reference a worksheet by its position?
INDIRECT needs a text reference that identifies the worksheet, typically by name. It cannot directly turn a worksheet index into a sheet reference.
What does =ByIndex(3,"A1") return?
It returns cell A1 from the third worksheet in the workbook containing the formula. Hidden worksheets count, but chart sheets do not.
Why is the ByIndex function not working after I reopen the workbook?
Check that the function is in a standard VBA module, the file is saved as an XLSM workbook, and macros are enabled. Use Enable Content only if you trust the file.
Will the result change when I reorder worksheets?
The function refers to the worksheet currently at the specified position. If the displayed result does not update after a move, select Formulas > Calculate Now.
What happens if the worksheet index is out of range?
The function cannot find the worksheet and the formula returns an error. Change the index to a number within the workbook's Worksheets collection.




