How to Use IFERROR with VLOOKUP in Autofill VBA | WPS Spreadsheet
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.

- 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 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.
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.
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.
Set your target range based on the calculated last row instead of selecting the cells. For example: Set DestRange = Range("C2:C" & LastRow).
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),"""")".

Use Excel Tables to Auto-Expand Formulas (No VBA Required)
If you want to avoid VBA entirely, converting your data into an official Spreadsheet Table will automatically autofill formulas to new rows.
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. Open WPS Spreadsheet: Launch WPS Office and open your workbook containing the data you wish to automate.
- 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. Open the VBA Editor: Click on 'VBA Editor' or press ALT+F11 to open the Visual Basic macro development environment.
- 4. Insert the Macro Code: Insert a new module and paste your VBA code, utilizing the dynamic range logic and doubled quotation marks.
- 5. Run the Macro: Save your script, close the editor, and click 'Macros' in the Developer tab to execute your automated VLOOKUP script.

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.




