Unable to Get the VLOOKUP Property of the WorksheetFunction Class

The error message “Unable to get the VLOOKUP property of the WorksheetFunction class” is commonly encountered when using VBA (Visual Basic for Applications) to automate tasks in Microsoft Excel. This error can be frustrating, as it often interrupts automated processes and requires manual debugging.

In this guide, we’ll break down the various reasons you might encounter this error and provide targeted solutions for each scenario. Whether you’re a seasoned VBA developer or new to Excel automation, this guide will help you navigate this common issue.

Understanding the Error Message

Before diving into the solutions, let’s understand what this error message means.

What is VLOOKUP?

VLOOKUP is a built-in Excel function that searches for a value in the first column of a range and returns a value in the same row from another column. VLOOKUP can be used directly within Excel worksheets or programmatically via VBA.

WorksheetFunction Class

In VBA, the WorksheetFunction class provides access to many Excel worksheet functions, including VLOOKUP. When you encounter the error, it means VBA couldn’t execute the VLOOKUP function for some reason.

The Error Message

The error message usually pops up in a dialog box, interrupting the execution of the VBA macro. It is an indication that something went wrong while trying to get the VLOOKUP property from the WorksheetFunction class.

Common Scenarios for the Error

Understanding the contexts in which this error occurs can help us get closer to a solution. Here are some common scenarios where you might encounter this issue:

Lookup Value Not Found

The most common reason for this error is that the value you’re trying to look up doesn’t exist in the lookup range. VLOOKUP is not forgiving in this regard; if it doesn’t find the exact match, it will return an error.

Range Issues

Improperly defined or dynamic ranges can cause VLOOKUP to malfunction. For instance, if your lookup range changes frequently, your VBA code may not be able to adapt to those changes automatically.

Data Type Mismatch

Data types need to be consistent between the lookup value and the lookup column. If one is a number and the other is text (even if it looks like a number), VLOOKUP will not be able to make a match.

Workbook or Worksheet Not Accessible

If the workbook or worksheet containing the lookup range is closed or not accessible for some reason, the VLOOKUP function will not be able to execute.

Nested Functions or Complex Formulas

Sometimes, the VLOOKUP function is part of a more complex formula or is nested within other functions. Errors in those other functions can propagate and cause the VLOOKUP to fail.

Multiple Instances of Excel

If you have multiple instances of Excel running, VBA might get confused about which instance it should refer to, leading to errors.

Debugging Techniques

Once you’ve identified the possible scenarios for the error, the next step is to debug your VBA code to pinpoint the issue. Here are some techniques to help you do that:

1 Use Breakpoints

Breakpoints allow you to pause the execution of your VBA code at a specific line. This can be incredibly useful for examining the state of your variables and ranges before the error occurs.

To set a breakpoint:

  1. Open the VBA editor (Alt + F11).
  2. Locate the line of code where the VLOOKUP function is called.
  3. Click on the gray margin to the left of the line number, or press F9.

2 Examine Variables with the Immediate Window

The Immediate Window (Ctrl + G in the VBA editor) is your best friend for quick debugging. You can print the values of variables and evaluate expressions to check if they’re what you expect.

For example, you can type:

? Worksheets("Sheet1").Range("A1").Value

This will output the value in cell A1 of Sheet1.

3 Step Through Code

Use the F8 key to step through your code line by line. This allows you to see exactly where the error occurs and examine the state of your program at that point.

4 Use Debug.Print for Logging

You can use the Debug.Print statement to print messages or variable values to the Immediate Window. This can help you track the flow of the program and the state of variables at various points.

Debug.Print "The value of myVariable is: " & myVariable

5 Handle Errors with On Error Statements

You can use On Error statements to handle errors gracefully. For example, you can redirect the code execution to a specific label when an error occurs:

On Error GoTo ErrorHandler
' Your VLOOKUP code here

Exit Sub

  MsgBox "An error occurred: " & Err.Description

6 Check Data Types

Ensure that the data types of your lookup values match those in the lookup columns. Use functions like IsNumeric or TypeName to check the data types.

Potential Solutions

After you’ve pinpointed the issue through debugging, you can proceed to implement a solution. Here are some potential ways to fix the “Unable to get the VLOOKUP property of the WorksheetFunction class” error.

1 Check for Existence of Lookup Value

Before running VLOOKUP, you can check if the value you’re looking for actually exists in the lookup column.

If IsError(Application.Match(lookupValue, Worksheets("Sheet1").Range("A:A"), 0)) Then
    MsgBox "Lookup value not found."
    Exit Sub
End If

2 Use Dynamic Ranges

If your lookup range changes frequently, consider using a dynamic named range or resize the range using VBA.

Set dynamicRange = Worksheets("Sheet1").Range("A1").CurrentRegion

3 Handle Data Type Mismatch

If you suspect that the data types are mismatched, use VBA functions to explicitly convert them.

lookupValue = CStr(lookupValue)
' or
lookupValue = CDbl(lookupValue)

4 Check Workbook or Worksheet Accessibility

Ensure the workbook or worksheet you’re referencing is open and accessible. You can use VBA to check if it’s open or even open it programmatically.

If Not IsWorkbookOpen("MyWorkbook.xlsx") Then
    Workbooks.Open ("C:\Path\To\MyWorkbook.xlsx")
End If

5 Simplify Nested Functions

If your VLOOKUP is part of a more complex formula, try breaking down the formula into smaller parts and test each one separately.

6 Use Application.VLookup Instead of WorksheetFunction.VLookup

Using Application.VLookup allows your code to continue running even if an error occurs, unlike WorksheetFunction.VLookup, which halts the code.

Dim result As Variant
result = Application.VLookup(lookupValue, lookupRange, columnNumber, False)

If IsError(result) Then
    MsgBox "Error in VLookup"
    MsgBox "The result is " & result
End If

7 Use Error Handling

You can wrap your VLOOKUP call in a Try...Catch block (in languages that support it) or use On Error statements in VBA to handle errors gracefully.

Best Practices to Avoid the Error

Prevention is often better than cure. Here are some best practices that can help you avoid running into the “Unable to get the VLOOKUP property of the WorksheetFunction class” error in the first place.

1 Validate Data Before Lookup

Always check the integrity of your data before performing a lookup. This includes making sure there are no duplicates, blanks, or inconsistencies in the data types.

2 Use Error-Handling Mechanisms

Incorporate error-handling mechanisms like On Error Resume Next or On Error GoTo ErrorHandler to catch and handle errors before they interrupt the workflow.

3 Use Option Explicit

Always use Option Explicit at the beginning of your VBA modules. This forces you to declare all variables, which can help catch typos and other mistakes that might lead to errors.

4 Use Meaningful Variable Names

Clear, descriptive variable names can help you and others understand the purpose of variables, making it easier to debug and maintain the code.

5 Keep Functions Simple

The simpler the function, the easier it is to debug. If a formula becomes too complex, consider breaking it down into smaller, more manageable pieces.

6 Test with Different Data Sets

Always test your VBA macros with multiple data sets to ensure they can handle a variety of scenarios. This can help you catch edge cases that you might not have considered.

7 Regularly Update Code

Keep your code updated and refactor it as needed. Over time, data structures and requirements can change, and your code should adapt to these changes.

8 Document Your Code

Comment your code adequately to explain what each section is doing. This not only helps you but also aids others who might be working on the same codebase.

9 Use a Version Control System

Using a version control system like Git allows you to track changes, collaborate more effectively, and revert to previous versions if a new change introduces errors.


The “Unable to get the VLOOKUP property of the WorksheetFunction class” error in Excel VBA can be a stumbling block in your automation tasks, but understanding its root causes and knowing how to debug effectively can go a long way in resolving it.

In this guide, we’ve covered:

  • Understanding the Error Message: What VLOOKUP and the WorksheetFunction class are, and what the error message means.
  • Common Scenarios for the Error: Situations where this error is likely to occur, such as lookup value not found, range issues, and data type mismatches.
  • Debugging Techniques: Various ways to debug the error, including using breakpoints, the Immediate Window, stepping through code, and error handling.
  • Potential Solutions: Targeted fixes for the error based on different scenarios, such as checking for the existence of the lookup value, handling data types, and using dynamic ranges.
  • Best Practices to Avoid the Error: Preventive measures like validating data, error handling, using meaningful variable names, and documentation.

By understanding the issue thoroughly and applying these debugging techniques and solutions, you can make your VBA code more robust and resistant to this particular error. And remember, the best code is not just the one that works, but the one that can be easily understood and maintained.

Related Posts: