Pass a Row Number Between Excel VBA UserForms
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.
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.
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.
In the VBA editor, open the first UserForm's code module and add `Public FoundRow As Long` at the top, outside any procedure.
In the first form's search procedure, assign the row after confirming that a matching cell was found, for example: `Me.FoundRow = foundCell.Row`.
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.
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.
Store the row number in a standard VBA module
Use a public module variable when multiple forms or procedures need to access the same selected row.
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.

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.




