How to Find an Excel VBA Row with Exact or Partial Criteria
Question details
The user needs an Excel VBA function to locate a specific row based on an exact board type, an optional subsystem number, and fallback rules for blank or partial matches.

- Product
- Excel
- Device & OS
- not provided
- Scenario
- Writing a VBA macro to search a dataset for rows matching multiple criteria across different columns, involving complex exact and partial matching logic.
- Observed behavior
- Requires a dynamic loop to evaluate multiple rows and apply specific priority logic when several partial or blank matches exist.
Before writing the script, clearly define your matching priority rules (e.g., whether exact matches always supersede partial ones) and save your workbook as a Macro-Enabled Spreadsheet (.xlsm).
Use a Dynamic Loop with Last Row Logic
Dynamically determine the last used row in your dataset to efficiently loop through and compare criteria without hardcoding cell ranges.
To avoid errors when your dataset expands or shrinks, use the End(xlUp) method to find the last row containing data. Inside a loop, you can evaluate each row against your exact or partial criteria.
Use the code `lastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row` to find the last populated row in the first column of your worksheet.
Create a loop using `For i = 2 To lastRow` (assuming row 1 is for headers) to iterate through the data range.
Inside the loop, write `If` statements to compare the board type and subsystem cell values (e.g., `ws.Cells(i, 1).Value`) against your required input.

Concatenate Columns for Combined Matching
Combine the values of multiple columns into a single string for a simplified, unified comparison.
Write and Execute VBA Macros Easily in WPS Spreadsheet
WPS Office provides robust support for VBA and Macros, allowing you to seamlessly run your custom row-finding scripts just as you would in Microsoft Excel.
- 1. Open your macro workbook: Launch WPS Spreadsheet and open your .xlsm file containing the data.
- 2. Access the Developer Tab: Go to the 'Developer' tab on the top ribbon.
- 3. Open the VBA Editor: Click 'Visual Basic' or 'VB Editor' to open the coding environment.
- 4. Insert your custom logic: Paste your custom row-finding VBA function into a new or existing module.
- 5. Run the Macro: Execute the macro to automatically search and identify rows based on your exact or partial criteria.

Frequently Asked Questions
How do I handle case-sensitive matches in VBA?
By default, VBA string comparisons are case-sensitive. To ignore text casing, you can add `Option Compare Text` to the top of your VBA module, or use the `UCase` or `LCase` functions on both strings during the actual comparison.
Can I use the Range.Find method instead of a loop?
Yes, `Range.Find` is generally faster for simple, single-column searches. However, when evaluating complex criteria across multiple columns and incorporating fallback rules for partial matches, a loop offers much better control over the logical flow.
What happens if there are empty rows interspersed within the dataset?
Using the dynamic `ws.Cells(ws.Rows.Count, 1).End(xlUp).Row` logic correctly identifies the very last populated row in a column. This ensures your loop will cover the entire active dataset, bridging across any empty rows that might appear in the middle.




