How to Search for UserForm TextBox Values Across Worksheets in Excel VBA
Question details
The user needs an Excel VBA macro that searches multiple worksheets for a part number entered into a UserForm TextBox and warns if duplicates are found.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Data entry and validation using VBA UserForms across a multi-sheet workbook.
- Observed behavior
- The macro should fetch the value from TextBox1, check that it is not blank, search all designated sheets, and notify the user with the exact cell address to inspect if a duplicate entry exists.
Ensure your workbook is saved as a Macro-Enabled Workbook (.xlsm) and that your VBA project includes a UserForm containing a TextBox (named TextBox1) and a CommandButton.
Implement a VBA Search and Duplicate Warning Script
Use a CommandButton click event to read the TextBox value, iterate through all worksheets using the Find method, and prompt the user upon detecting duplicate part numbers.
To accomplish this, you must loop through your target worksheets and apply the Range.Find method on each sheet. By capturing the click event of a button on the UserForm, you can halt the duplicate entry and let the user visually inspect the existing record.
Press Alt + F11 to open the VBA Editor, locate your UserForm in the Project Explorer, right-click it, and select 'View Code'.
Select your CommandButton from the top-left dropdown in the code window to generate a 'Private Sub CommandButton1_Click()' event.
Inside the click event, declare a variable and assign it using 'Trim(Me.TextBox1.Value)'. Add an If statement to check if the value is blank, and exit the sub if it is.
Use a 'For Each ws In ThisWorkbook.Worksheets' loop. Inside the loop, use 'ws.Cells.Find(What:=yourValue)' to search for the TextBox value.
If a match is found (Not matchRange Is Nothing), display a MsgBox with the worksheet name and cell address. Ask if they want to inspect it. If yes, use 'ws.Activate', 'matchRange.Select', and 'Me.Hide' to let the user review the duplicate.

Create and Manage VBA UserForms with WPS Spreadsheets
WPS Spreadsheets provides robust support for VBA macros, enabling you to build complex UserForms, automate multi-sheet searches, and validate your data directly within a lightweight workspace.
- 1. Open Your Macro Workbook: Launch WPS Spreadsheets and open your .xlsm file containing the UserForm.
- 2. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click on 'Visual Basic' or 'WPS Macro' to access the built-in VBA editor.
- 3. Edit Your UserForm Logic: Locate your UserForm code and paste or edit your multi-sheet search and duplicate-warning script.
- 4. Run and Validate: Click 'Run' or trigger the UserForm from your worksheet to ensure the duplicate part number warnings function perfectly.

Frequently Asked Questions
Why should I use Trim() when reading TextBox values in VBA?
Using the Trim() function removes any accidental leading or trailing spaces entered by the user. This ensures that the Find method searches for the exact text characters and doesn't fail due to invisible spaces.
How do I hide the UserForm to inspect the worksheet?
You can use the command 'Me.Hide' (or 'UserFormName.Hide') in your VBA code. This temporarily hides the dialog box, granting you full access to navigate and inspect the selected worksheet cells behind it.
Can the VBA Find method search across all worksheets automatically?
No, the Find method only operates on a specific range or a single worksheet at a time. To search an entire workbook, you must construct a loop (such as 'For Each ws In Worksheets') and run the Find command on each sheet sequentially.




