VLOOKUP Formula in Excel VBA

Two ways to run a lookup from a macro instead of typing it manually

There are two distinct ways to use VLOOKUP from VBA: writing the VLOOKUP formula as text into a cell using code, so it behaves exactly like a manually typed formula, or calling Application.WorksheetFunction.VLookup directly to get a result back into a VBA variable without touching a cell at all.

This page covers both approaches using a small employee table, since which one to use depends on whether the result needs to live in a cell or just inside the macro's logic.

Turbo Excel VLookup

Turbo Excel VLookup
App Screenshots
Lookup and Merge tab - VLOOKUP XLOOKUP replacement
Match mode and return option selection
Lookup statistics and match percentage report
Join tab - Left Right Inner Full Outer Anti Semi Cross join
Duplicates tab - duplicate record finder
Data Cleaning tab - trim normalize and clean columns
Formula Generator tab - VLOOKUP XLOOKUP INDEX MATCH formula builder
Reports tab - session log of lookups joins and exports
Export options - XLSX CSV TSV output settings
Key Column Pairs - multi-key composite matching setup
License activation screen - Gumroad key entry
Help tab - built-in usage guide and tips
Enlarged application screenshot
Approach 1: Writing the Formula Into a Cell
Employee IDNameDepartmentSalary
E101Asha RaoMarketing54000
E102Vikram ShahFinance61000
E103Priya NairSales48000

This code inserts a normal VLOOKUP formula into cell F2, the same as typing it manually:

Range("F2").Formula = "=VLOOKUP(E2, A2:D4, 3, FALSE)"
Approach 2: Getting a Value Directly Into VBA

This approach skips the worksheet cell entirely and stores the result in a VBA variable, useful when the lookup result feeds into further macro logic rather than being displayed directly:

Dim dept As Variant
dept = Application.WorksheetFunction.VLookup("E102", Range("A2:D4"), 3, False)
Handling a Missing Match in VBA

WorksheetFunction.VLookup raises a runtime error if no match is found, rather than returning #N/A silently, so it needs to be wrapped in error handling:

On Error Resume Next
dept = Application.WorksheetFunction.VLookup("E999", Range("A2:D4"), 3, False)
If Err.Number <> 0 Then dept = "Not Found"
On Error GoTo 0
Looping VLOOKUP Across Many Rows

For applying the same lookup down an entire column programmatically, a loop combined with the WorksheetFunction approach avoids inserting hundreds of individual cell formulas:

For i = 2 To lastRow
  Cells(i, 6).Value = Application.WorksheetFunction.VLookup(Cells(i, 5).Value, Range("A2:D" & lastRow), 3, False)
Next i
Matching a Full Dataset Without Writing Macro Code

For anyone who reached for VBA mainly to avoid manually copying a formula down hundreds of rows, Turbo Excel VLookup achieves the same end result - matching an entire dataset in one action - without needing any macro code written or maintained.

Speeding Up VLOOKUP in a Large Workbook

A worksheet with thousands of VLOOKUP formulas recalculating against a large table_array can slow Excel down noticeably, especially if the range isn't locked and Excel has to re-evaluate a shifting reference on every keystroke.

Converting the source range into an Excel Table (Insert > Table) before referencing it in VLOOKUP tends to help, since Table references automatically resize with the data and stay consistent without needing manual $ locking.

Building the Habit of Testing With One Row First

Before applying a new VLOOKUP formula to an entire column, it's worth testing it on a single row and manually verifying the result against the source data, since a mistake caught in one cell takes seconds to fix, while the same mistake copied down five hundred rows takes much longer to untangle.

This is especially worth doing whenever the source table_array has changed shape recently, since a resized or reordered table is the single most common reason a previously reliable formula starts returning unexpected results.

Frequently Asked Questions

What's the difference between writing a formula and using WorksheetFunction.VLookup in VBA?

Writing a formula inserts it into a cell as text; WorksheetFunction.VLookup returns a value directly into a VBA variable.

Does WorksheetFunction.VLookup handle a missing match the same way as a normal formula?

No, it raises a runtime error rather than returning #N/A, so it needs explicit error handling.

How do I handle errors from WorksheetFunction.VLookup?

Use On Error Resume Next before the call and check Err.Number afterward to detect and handle a missing match.

Can VBA loop VLOOKUP across an entire column?

Yes, a For loop combined with WorksheetFunction.VLookup can apply the lookup to each row without inserting individual cell formulas.

Is inserting a formula as text the same as typing it manually?

Yes, once inserted it behaves exactly like a normal formula and updates if the source data changes.

Is there a way to avoid writing VBA code for this kind of matching?

Yes, Turbo Excel VLookup matches a full dataset through its interface without requiring any macro code.

Does locking a range with F4 change how the formula calculates?

No, it only fixes the range so it doesn't shift when the formula is copied - the calculation itself works the same either way.

What's a fast way to check what a formula is actually returning?

Selecting part of the formula in the formula bar and pressing F9 shows its calculated value directly, without changing the formula.

Can trailing spaces in a cell cause a lookup to fail?

Yes, an extra space at the end of a value is invisible but prevents an exact match; TRIM() around the lookup value often fixes this.

Does the order of rows in the source data matter for an exact match?

No, an exact match search works regardless of row order; only an approximate match requires sorted data.

Ready to stop fighting #N/A errors and match your spreadsheets visually?

Related guides and tools

This guide is part of TurboSoft’s Excel lookup and matching resources. Turbo Excel Lookup handles all of these tasks offline on Windows.

Get Turbo Excel Lookup →