Extract a Row Number from an Excel Formula with VBA
Question details
Find a changing row number in an Excel formula or manage the row separately to build a dynamic reference.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A workbook uses formulas with cell references whose row numbers can change.
- Observed behavior
- The user wants to identify or update the intended row reference without changing unrelated numbers.
Check whether the formula is a simple cell reference or contains functions, sheet names, or external links. If you use the VBA example, make sure macros are permitted and save the workbook as an Excel Macro-Enabled Workbook (.xlsm).
Extract the row from a simple Excel formula with VBA
Use this Windows VBA example when a cell contains only a direct A1-style reference, such as =A12 or =$A$12.
The regular expression is anchored to the entire formula, so it does not extract numbers from more complex formulas. It does not support sheet-qualified references, external links, ranges, or formulas with functions.
In Excel for Windows, press Alt+F11, select your workbook in the Project pane, and choose Insert > Module.
Paste this code into the module: Function RowFromFormula(target As Range) As Variant Dim re As Object, matches As Object Application.Volatile Set re = CreateObject("VBScript.RegExp") re.Pattern = "^=\$?[A-Za-z]{1,3}\$?([1-9][0-9]*)$" Set matches = re.Execute(target.Formula) If matches.Count > 0 Then RowFromFormula = CLng(matches(0).SubMatches(0)) Else RowFromFormula = CVErr(xlErrNA) End If End Function
Return to the worksheet and enter =RowFromFormula(C1), replacing C1 with the cell containing the formula. If C1 contains =$A$12, the result is 12.

Build a dynamic reference from a utility sheet
Use INDIRECT when you can store the sheet, column, and row separately instead of extracting them from an existing formula.
Manage changing row references in WPS Spreadsheet
Use a utility sheet and INDIRECT in WPS Spreadsheet to build a reference from a sheet name, column, and row without extracting numbers from formula text.
- 1. Open the workbook: Open your spreadsheet in WPS Office and select the Spreadsheet workbook.
- 2. Add utility values: Create a sheet named Utility. Enter the target sheet name in B1, its column letter in B2, and its row number in B3.
- 3. Enter the dynamic formula: In the destination cell, enter =INDIRECT("'"&SUBSTITUTE(Utility!B1,"'","''")&"'!"&Utility!B2&Utility!B3).
- 4. Update the reference: Change Utility!B3 to point to a different row, then check the destination cell for the new value.

Frequently Asked Questions
Why does the VBA function return #N/A?
The example only accepts a formula consisting of a single direct A1-style reference, such as =A12. Sheet names, external links, ranges, and functions do not match its pattern.
How do I preserve the VBA function when saving?
Choose File > Save As and select Excel Macro-Enabled Workbook (.xlsm). A standard .xlsx file cannot retain VBA code.
Can INDIRECT reference a closed external workbook?
In Excel, INDIRECT requires an external source workbook to be open. The utility-sheet example uses a sheet in the current workbook instead.
Does the utility-sheet approach extract a row from an existing formula?
No. It stores the row number in Utility!B3 and uses that value to construct a new reference. To extract a row from existing formula text, use a suitable VBA pattern.




