How to Find an Excel Row by Matching Two Columns with VBA
Question details
The user needs a VBA solution to locate a specific row by matching a board type in one column and performing a partial match for a subsystem in a second column, with a fallback to a blank cell if no subsystem match is found.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Writing a VBA macro to search and retrieve data based on multiple column criteria, specifically handling partial string matches and blank fallbacks.
- Observed behavior
- Requires a functional VBA script or logic to successfully evaluate both columns and return the target row number or value when the specific conditions are met.
Before writing your macro, ensure that the Developer tab is enabled in your ribbon and take note of the exact column indices for your board type and subsystem data.
Use a VBA Loop with the InStr Function
Iterate through the rows to check both columns sequentially, using the InStr function to perform a case-insensitive partial match on the second column.
This method involves looping through all relevant rows in your dataset. It first checks for an exact match in the board type column. If found, it uses the InStr function to evaluate if the subsystem string contains the search value. It also includes logic to return the row if the subsystem cell is blank.
Press ALT + F11 to open the Visual Basic for Applications (VBA) editor and insert a new module via Insert > Module.
Write a 'For' or 'Do While' loop to iterate through the data rows of your target worksheet.
Use an 'If' statement to check if the value in the first column exactly matches your 'board type' variable.
Inside the first condition, nest a second 'If' statement using InStr: 'If InStr(1, CStr(ws.Cells(rowNumber, 2).Value), subsysnum, vbTextCompare) > 0 Then'. This checks for a partial match.
Add an 'ElseIf' statement to handle cases where the partial match fails but the cell is empty: 'ElseIf ws.Cells(rowNumber, 2).Value = "" Then'.

Use the VBA Range.Find Method with Wildcards
Utilize Excel's built-in Find function within VBA using asterisk wildcards to locate partial matches efficiently.
Combine Columns Using a Helper Column
If you want to simplify your VBA code, concatenate the two columns directly in the worksheet and search the combined column.
Automate Complex Data Lookups with WPS Spreadsheet
WPS Office provides robust, built-in support for VBA and macros. You can seamlessly run scripts to match multiple columns, handle partial text searches, and automate your entire workflow within a familiar interface.
- 1. Open Your Workbook: Launch WPS Spreadsheet and open the file containing your data.
- 2. Access the Developer Tools: Navigate to the 'Tools' or 'Developer' tab on the top ribbon.
- 3. Open the VBA Editor: Click on 'Visual Basic' or press ALT + F11 to launch the macro editor.
- 4. Insert Your Code: Insert a new Module, paste your column-matching VBA script utilizing InStr or Range.Find, and save the workbook as a macro-enabled format.
- 5. Run the Macro: Click 'Run' or trigger the macro via a customized button to locate the matching row instantly.

Frequently Asked Questions
Can I use multiple criteria directly with the VBA Find method?
The Range.Find method natively supports searching for a single value. To match multiple columns simultaneously, you must either search one column and validate the other using the Offset property, or use an iterative loop.
Why is my InStr function failing to find a partial match?
Ensure you are using 'vbTextCompare' as the final argument for case-insensitive searches. Also, use 'CStr()' to convert the cell value to a string, which prevents type mismatch errors when dealing with numeric or formatted cells.
How do I prevent runtime errors if the row is not found in VBA?
Always check if the result of a Range.Find operation 'Is Nothing' before attempting to access its row or value properties. For loops, ensure you have a variable that tracks whether a match was found, and handle the logic if the loop finishes without a match.




