How to Speed Up VBA Tests for Values in a List in Excel
Question details
The user needs an efficient way to check if values in a large dataset belong to a fixed list without writing formulas or checking cells one by one, which currently causes the macro to run very slowly.

- Product
- Excel / WPS Spreadsheet
- Device & OS
- not provided
- Scenario
- Running a VBA macro to validate or cross-reference thousands of rows of data against a fixed list of lookup values.
- Observed behavior
- Processing the dataset takes an excessively long time due to inefficient use of IF statements and writing formulas back to the worksheet cell by cell.
Save a backup copy of your workbook before executing new VBA code, as macro operations that overwrite range data cannot be reversed using the standard Undo feature.
Use Memory Arrays and the Scripting.Dictionary Object
This method minimizes the slow interaction between VBA and the worksheet by reading all data into memory, checking values against a fast hash table, and writing the results back simultaneously.
Reading from and writing to worksheet cells individually is the primary cause of slow macro performance. By moving your dataset into an in-memory Array, you eliminate this bottleneck.
Combining an Array with a `Scripting.Dictionary` provides an ultra-fast way to check if an item exists in your fixed list, making it highly suitable for large datasets.
Create a `Scripting.Dictionary` object using `CreateObject("Scripting.Dictionary")`. Loop through your fixed lookup list range and add each unique value as a dictionary key.
Assign your target worksheet range directly to a Variant variable (e.g., `arrData = Range("A2:A100000").Value`). This instantly copies the entire range into memory.
Set up a second Array of the exact same size to hold your output. Use a `For` loop to iterate through the data array. Use the `Dictionary.Exists(arrData(i, 1))` method to instantly verify if the value belongs to the fixed list, and store the result in your output array.
Once the loop is complete, assign your output array back to the worksheet range (e.g., `Range("B2:B100000").Value = arrOutput`).

Use Arrays with a Select Case Statement
If your fixed list only contains a few static values that rarely change, a Select Case statement combined with arrays avoids the need to set up a Dictionary while remaining highly efficient.
Run Fast and Reliable VBA Macros with WPS Spreadsheet
WPS Office offers robust VBA support, allowing you to seamlessly execute advanced macro scripts, Dictionary objects, and Array operations. If you work with large datasets, WPS Spreadsheet processes your optimized VBA code rapidly while maintaining perfect format compatibility.
- 1. Open your workbook in WPS Spreadsheet: Launch WPS Office and open the .xlsm or .xlsb file containing your large dataset.
- 2. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click on 'Visual Basic', or press Alt + F11 to launch the macro editor.
- 3. Insert your optimized Array code: Insert a new Module, paste your Array and Dictionary optimization code, and press F5 to execute the fast lookup test.

Frequently Asked Questions
Why does writing formulas cell-by-cell slow down my VBA macro?
Every time VBA reads from or writes to a worksheet cell, it must cross the boundary between the VBA environment and the spreadsheet application. Doing this thousands of times inside a loop causes immense overhead, resulting in severe slowdowns. Arrays process data entirely within system memory.
Is using Application.WorksheetFunction.Match faster than a Dictionary?
While using the Match function in VBA is faster than a standard loop over cells, it is significantly slower than a Scripting.Dictionary when iterating over massive arrays. The Dictionary object uses hash tables, making value lookups nearly instantaneous regardless of the list size.
Do I need to enable specific references to use a Dictionary in VBA?
You can use 'Late Binding' by creating the object directly with `CreateObject("Scripting.Dictionary")` without enabling any references. However, if you prefer 'Early Binding' to utilize auto-complete features while writing code, you must enable the 'Microsoft Scripting Runtime' in the VBA Editor via Tools > References.




