VBA & Macro Problems

Pass a Row Number Between Excel VBA UserForms

Algirdas JasaitisAlgirdas Jasaitis Nov 14, 2026 869 views

Question details

Share the selected row number from a search UserForm with an editing UserForm in Excel VBA.

Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A first UserForm searches for a record, and a second UserForm edits the record at the selected row.
Observed behavior
The second UserForm may not recognize the row number stored in the first form.
Before you start

Open the VBA editor with Alt+F11 and identify the names of both UserForms and the variable holding the row number. Confirm that the search found a record before assigning or using its row.

Solution 1Recommended

Reference the public variable on the first UserForm

Use this approach when the row number belongs to the search form and the editing form needs to read it.

1
Declare the row number

In the VBA editor, open the first UserForm's code module and add `Public FoundRow As Long` at the top, outside any procedure.

2
Store the search result

In the first form's search procedure, assign the row after confirming that a matching cell was found, for example: `Me.FoundRow = foundCell.Row`.

3
Read it from the second form

In the second UserForm's code, reference the first form by name, for example: `rowToEdit = UserForm1.FoundRow`. Replace `UserForm1` with the actual name of the first form.

4
Test the form sequence

In Excel, run the search, select a record, and open the editing form. Verify that it reads the expected row before it changes any cells.

Use the correct form instance: `UserForm1.FoundRow` reads the default instance of UserForm1. If your code created the search form with `New`, use a reference to that same instance or pass its row number to the editing form explicitly.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for working with spreadsheets and migrating everyday files. It does not fix Excel VBA issues, and macro support varies by WPS version and configuration.

Open and edit Excel-compatible XLSX spreadsheets.Use a familiar spreadsheet interface to review and edit records.Migrate everyday Office documents without learning an entirely new workflow.Use free core tools in a lightweight office suite.
microsoft office alternative - wps office

Frequently Asked Questions

Why can't the second UserForm see FoundRow?

A variable declared inside a procedure is local to that procedure. Declare it as `Public` at the top of the first UserForm module and access it as `UserForm1.FoundRow`, or declare it in a standard module and access it directly.

What if I created the first UserForm with New?

A form created with `New UserForm1` is not necessarily the default instance referenced by `UserForm1.FoundRow`. Use a reference to the actual search form instance or pass the row number to the editing form.

Which data type should I use for an Excel row number?

Use `Long`, as in `Public FoundRow As Long`. Excel row numbers can exceed the range of the VBA `Integer` type.

What happens if the search does not find a record?

Check the result before reading its `Row` property or opening the editing form. For example, when using `Range.Find`, test whether the returned range is `Nothing` and show a message if no match was found.