VBA & Macro Problems

How to Find an Excel Row by Matching Two Columns with VBA

Nimra MalikNimra Malik Oct 28, 2026 869 views

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.

How to Find an Excel Row by Matching Two Columns Using VBA
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 you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 to open the Visual Basic for Applications (VBA) editor and insert a new module via Insert > Module.

2
Create the Loop

Write a 'For' or 'Do While' loop to iterate through the data rows of your target worksheet.

3
Evaluate the First Column

Use an 'If' statement to check if the value in the first column exactly matches your 'board type' variable.

4
Perform a Partial Match on the Second Column

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.

5
Add Blank Fallback Logic

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 a VBA Loop with the InStr Function
Case Insensitivity: Using 'vbTextCompare' in the InStr function ensures that the partial match ignores uppercase and lowercase differences, making your search more robust.
Advanced Spreadsheet Macros

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. 1. Open Your Workbook: Launch WPS Spreadsheet and open the file containing your data.
  2. 2. Access the Developer Tools: Navigate to the 'Tools' or 'Developer' tab on the top ribbon.
  3. 3. Open the VBA Editor: Click on 'Visual Basic' or press ALT + F11 to launch the macro editor.
  4. 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. 5. Run the Macro: Click 'Run' or trigger the macro via a customized button to locate the matching row instantly.
Fully compatible with Microsoft Excel VBA scripts and macrosAutomate complex data lookups across multiple columns easilyLightweight program with fast execution speedsFree access to powerful spreadsheet analysis tools
microsoft office alternative - wps office

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.