VBA & Macro Problems

Extract a Row Number from an Excel Formula with VBA

Muhammad TalhaMuhammad Talha Dec 2, 2026 869 views

Question details

Find a changing row number in an Excel formula or manage the row separately to build a dynamic reference.

Extract a Row Number from an Excel Formula with VBA
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.
Before you start

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).

Solution 1Recommended

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.

1
Open the VBA editor

In Excel for Windows, press Alt+F11, select your workbook in the Project pane, and choose Insert > Module.

2
Add the extraction function

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

3
Use the 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.

Extract the row from a simple Excel formula with VBA
For linked or complex formulas: A formula such as ='[Sales.xlsx]Data'!A12 will return #N/A with this example. Use a pattern tailored to the known formula structure, or manage the reference on a utility sheet instead.
Work with dynamic references

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. 1. Open the workbook: Open your spreadsheet in WPS Office and select the Spreadsheet workbook.
  2. 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. 3. Enter the dynamic formula: In the destination cell, enter =INDIRECT("'"&SUBSTITUTE(Utility!B1,"'","''")&"'!"&Utility!B2&Utility!B3).
  4. 4. Update the reference: Change Utility!B3 to point to a different row, then check the destination cell for the new value.
Change the target row in one utility cell.Build references without a VBA macro.Open and save Microsoft Excel XLSX workbooks.Use a familiar spreadsheet interface.
microsoft office alternative - wps office

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.