VBA & Macro Problems

Reference an Excel Worksheet by Position Using a VBA Function

Steve KSteve K Dec 2, 2026 869 views

Question details

Reference a cell on an Excel worksheet by the worksheet's current position rather than its name.

Reference an Excel Worksheet by Position Using a VBA Function
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

In Excel, select Developer > Visual Basic, then select Insert > Module to add a standard module.

2
Add the function

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

3
Save as a macro-enabled workbook

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.

4
Enter the formula

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.

5
Recalculate after reordering

If you move worksheets and the result does not update, use Formulas > Calculate Now to recalculate the workbook.

Create a VBA function that references a worksheet by index
Check the index and cell address: The index must identify an existing worksheet, and the address must be valid. Hidden worksheets count toward the index, but chart sheets do not.
Free Microsoft Office alternative

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.

Edit worksheet cells and organize sheets in a familiar interfaceCompatible with Microsoft Excel formats including XLSXFree and lightweight for everyday spreadsheet workOpen existing Office files for a straightforward transition
microsoft office alternative - wps office

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.