Update Only the Selected Record in an Excel UserForm
Question details
Update only the record loaded into an Excel UserForm when multiple worksheet rows have the same name.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A UserForm searches worksheet records by name and lets the user edit a selected result.
- Observed behavior
- The Update button changes every row with the matching name instead of only the loaded record.
Open the workbook in Excel, save it as an Excel Macro-Enabled Workbook (.xlsm), and enable macros if prompted. In the VBA editor, identify the UserForm controls, worksheet name, and columns used by your records before adapting the example.
Store the Selected Worksheet Row in the UserForm
Use the row number associated with the chosen search result so the Update button writes to that row only.
This example assumes a worksheet named Records, names in column A, values in column B, and headers in row 1. The form uses txtSearch, lstResults, txtName, txtValue, btnSearch, and btnUpdate; change these names and columns to match your workbook.
The list stores each matching worksheet row in a hidden column. The row is saved only when the user selects a result, not while the search loops through matches.
Press Alt+F11, open the UserForm's code module, and add this declaration above its procedures: Private selectedRow As Long
In the same UserForm module, add or adapt the Search button procedure. It clears any previous selection and adds each matching row to lstResults: Private Sub btnSearch_Click() Dim ws As Worksheet Dim i As Long, lastRow As Long Set ws = ThisWorkbook.Worksheets("Records") selectedRow = 0 With lstResults .Clear .ColumnCount = 2 .ColumnWidths = "0 pt;120 pt" lastRow = ws.Cells(ws.Rows.Count, "A").End(xlUp).Row For i = 2 To lastRow If StrComp(Trim(CStr(ws.Cells(i, "A").Value)), Trim(txtSearch.Value), vbTextCompare) = 0 Then .AddItem CStr(i) .List(.ListCount - 1, 1) = ws.Cells(i, "A").Value End If Next i End With End Sub
Add a Click procedure for lstResults. When the user selects a result, it stores that result's worksheet row and loads its cells into the form: Private Sub lstResults_Click() Dim ws As Worksheet If lstResults.ListIndex < 0 Then Exit Sub Set ws = ThisWorkbook.Worksheets("Records") selectedRow = CLng(lstResults.List(lstResults.ListIndex, 0)) txtName.Value = ws.Cells(selectedRow, "A").Value txtValue.Value = ws.Cells(selectedRow, "B").Value End Sub
Replace the Update button's loop over matching names with a direct write to selectedRow: Private Sub btnUpdate_Click() Dim ws As Worksheet If selectedRow < 2 Then MsgBox "Select a search result first." Exit Sub End If Set ws = ThisWorkbook.Worksheets("Records") ws.Cells(selectedRow, "A").Value = txtName.Value ws.Cells(selectedRow, "B").Value = txtValue.Value MsgBox "Selected record updated." End Sub
Run the UserForm, search for a name that appears in multiple rows, select one result, and click Update after editing its fields. Check the Records worksheet to confirm that only the selected row changed.

Work with Excel Spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface for opening and editing Excel files. It supports common Excel formats for easier migration, though VBA availability and macro compatibility can vary by version and should be checked before relying on a UserForm.

Frequently Asked Questions
Why does my Update button change every record with the same name?
The button may loop through the worksheet and write to every row whose name matches the form. Store the selected row when the user loads a result, then write directly to that row instead.
How can users distinguish search results with the same name?
Add another identifying field, such as a record ID, to the UserForm's results list. Keep the worksheet row or unique ID associated with each list item so the selected result can be loaded correctly.
What happens if someone sorts the sheet while the form is open?
The stored row number may no longer identify the same record. Store a unique ID when loading the form and search for that ID again when the user clicks Update.
Why does the form say to select a search result first?
The example resets selectedRow when a new search starts. Click an item in lstResults to load a record before clicking Update.
How do I keep the UserForm code in my workbook?
In Excel, choose File > Save As and select Excel Macro-Enabled Workbook (.xlsm). Enable macros when prompted only if you trust the workbook.




