Function Problems

Fix Excel VBA VLOOKUP Errors and Duplicate Name Matches

Nimra MalikNimra Malik Nov 22, 2026 870 views

Question details

Find the correct record with VLOOKUP in VBA when names contain spaces or first names are duplicated.

Fix Excel VBA VLOOKUP Errors and Duplicate Name Matches
Product
Microsoft Excel VBA
Device & OS
not provided
Scenario
A workbook looks up names on another sheet and needs an exact match for each person.
Observed behavior
The lookup may return #N/A or #VALUE!, or match the wrong person when the lookup key is not unique.
Before you start

Check which columns contain the lookup names and return values on each sheet. Confirm that the lookup key is in the first column of the VLOOKUP table range.

Solution 1Recommended

Use an Exact-Match VLOOKUP in Excel VBA

Use this approach when the complete lookup value is available and you want VBA to handle a missing match.

The example uses A2 on a sheet named Lookup as the key and searches Time!A2:B12. Change the sheet names, cells, and range to match your workbook.

1
Check the worksheet formula

In Lookup!B2, test =VLOOKUP(A2,Time!$A$2:$B$12,2,FALSE). The final FALSE requires an exact match.

2
Add the VBA lookup

In the Visual Basic editor, add this code to a macro: Dim result As Variant result = Application.VLookup(Worksheets("Lookup").Range("A2").Value, Worksheets("Time").Range("A2:B12"), 2, False) If IsError(result) Then Worksheets("Lookup").Range("B2").Value = "Not found or lookup error" Else Worksheets("Lookup").Range("B2").Value = result End If

3
Run and verify the macro

Run the macro from the Visual Basic editor and compare Lookup!B2 with the corresponding record on the Time sheet. If it reports an error, check the key and table range.

Use an Exact-Match VLOOKUP in Excel VBA
Duplicate keys: VLOOKUP returns the first matching row. If multiple people have the same lookup key, create a more specific key before running the lookup.
Free Microsoft Office alternative

Work with Name Lookups in WPS Spreadsheet

WPS Office is a free, lightweight Office alternative with a familiar interface for opening Excel workbooks and working with lookup formulas. Move compatible spreadsheets into WPS Spreadsheet and test any VBA macros separately, as macro support may vary.

Use spreadsheet formulas to work with exact name lookupsOpen and edit compatible Microsoft Excel XLS and XLSX filesNavigate a familiar spreadsheet interfaceUse a free, lightweight Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Why does VLOOKUP return #N/A for a name that appears on the other sheet?

Check the complete lookup value against the first column of the table range. Extra spaces, nonbreaking spaces, or other hidden characters can prevent an exact match. Use FALSE as the final VLOOKUP argument.

What happens if two rows have the same lookup name?

VLOOKUP returns the first matching row. Combine first and last names in a helper column, or add a unique identifier if full names are also duplicated.

How can I handle a missing VLOOKUP match in VBA?

Assign the result of Application.VLookup to a Variant and check it with IsError before writing the result to a worksheet cell.

Why does the lookup return the wrong column or a #VALUE! error?

Check the VLOOKUP table range and column index. The index must be a valid positive number within the range, and the lookup key must be in the range's first column.