VBA & Macro Problems

How to Use IFERROR with VLOOKUP in Autofill VBA | WPS Spreadsheet

Nimra MalikNimra Malik Oct 28, 2026 875 views

Question details

The user needs a VBA macro to autofill a VLOOKUP formula wrapped in IFERROR across a dynamic destination range based on the number of rows in an adjacent column.

How to Use IFERROR with VLOOKUP in Autofill VBA
Product
Spreadsheet
Device & OS
not provided
Scenario
Writing a VBA macro to apply a formula to multiple cells without hardcoding the range or referencing an entire column, which causes errors and returns #N/A for blank lookup rows.
Observed behavior
Using a fixed range works, but applying the formula to an entire column causes IFERROR problems. Blank lookup rows return #N/A, and there are syntax issues when inputting the formula via VBA string.
Before you start

Before running your macro, make sure your document is saved as a Macro-Enabled Workbook (.xlsm) and that you have enabled Developer tools in your spreadsheet application.

Solution 1Recommended

Define a Dynamic Range and Format Quotation Marks Properly

To prevent #N/A errors and macro failures, dynamically calculate the last used row and ensure all quotation marks inside the VBA formula string are doubled.

Applying a formula to an entire column (like C:C) processes over a million rows, significantly slowing down your spreadsheet and triggering unnecessary IFERROR calculations for blank cells. Instead, macros should dynamically detect the last row of your dataset.

Additionally, when writing formulas containing quotation marks (such as returning a blank "" in an IFERROR statement) via VBA, you must double the quotation marks within the code. Finally, avoid using .Select or ActiveCell, as referencing the range directly is faster and less prone to errors.

1
Determine the last used row

Use VBA to find the last row in your reference column (e.g., Column B). You can achieve this by assigning a variable using the Rows.Count method: LastRow = Cells(Rows.Count, "B").End(xlUp).Row.

2
Define the destination range directly

Set your target range based on the calculated last row instead of selecting the cells. For example: Set DestRange = Range("C2:C" & LastRow).

3
Assign the properly formatted formula

Apply the formula directly to the defined range object. Ensure any embedded double quotes in the formula are typed twice. Your code should look similar to: DestRange.Formula = "=IFERROR(EXACT(VLOOKUP(A2,'Import'!A:D,4,0),B2),"""")".

Define a Dynamic Range and Format Quotation Marks Properly
Direct Object Referencing: By directly referencing DestRange.Formula instead of using Selection.Formula, your macro will execute instantly without screen flickering or unnecessary system resource consumption.
Efficient Spreadsheet Data Management

Automate Data Lookups Using Macros in WPS Spreadsheet

WPS Spreadsheet fully supports VBA macros, allowing you to seamlessly automate complex and repetitive data processing tasks, including dynamic VLOOKUP and IFERROR application.

  1. 1. Open WPS Spreadsheet: Launch WPS Office and open your workbook containing the data you wish to automate.
  2. 2. Access the Developer Tab: Navigate to the Developer tab on the top ribbon. If it is hidden, you can easily enable it in the application settings.
  3. 3. Open the VBA Editor: Click on 'VBA Editor' or press ALT+F11 to open the Visual Basic macro development environment.
  4. 4. Insert the Macro Code: Insert a new module and paste your VBA code, utilizing the dynamic range logic and doubled quotation marks.
  5. 5. Run the Macro: Save your script, close the editor, and click 'Macros' in the Developer tab to execute your automated VLOOKUP script.
Full support for advanced macros, VBA, and Developer tools.Seamlessly handles complex logic like nested VLOOKUP and IFERROR functions.Highly compatible with Microsoft Excel (.xlsx and .xlsm) macro-enabled formats.Lightweight architecture ensures high processing speed for large datasets.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my VBA formula string throw a syntax error when using IFERROR?

In VBA, strings are enclosed in double quotation marks. If your actual spreadsheet formula contains quotation marks (like "" to output a blank cell in an IFERROR statement), you must double them inside the VBA string (typing """") so VBA recognizes them as literal characters instead of the end of the text string.

How do I dynamically find the last used row in a column using VBA?

You can accurately find the last used row by counting backward from the bottom of the worksheet. The standard method is using the code: Cells(Rows.Count, "B").End(xlUp).Row, which returns the row number of the last non-empty cell in column B.

Why should I avoid using Select and ActiveCell in my macros?

Using .Select, .Selection, or .ActiveCell drastically slows down macro execution because it forces the application to continually update the user interface and shift cell focus. Directly referencing objects (e.g., Range("C2:C100").Formula) bypasses the interface, making the code much faster and more reliable.