Fix VBA VLOOKUP and MATCH Errors with Dates in Spreadsheets
Question details
The user needs to fix a crashing VBA function that fails to match a date and fund name against a named range when using VLOOKUP and MATCH.

- Product
- Spreadsheets
- Device & OS
- not provided
- Scenario
- Writing a VBA function to look up specific financial or fund data based on a date and name criteria from a master returns table.
- Observed behavior
- The date matching fails, and the subsequent row assignment or VLOOKUP operation causes the VBA function to crash.
Ensure your target data range is correctly defined as an ordinary Named Range (e.g., MasterReturnsTable) in the Name Manager, and check your VBA code for any spelling errors in variable declarations (such as correcting firsCol to firstCol).
Use CLng for Date Matching and Direct Cell Referencing
Convert the date to a Long data type using CLng so VBA correctly interprets the underlying numeric date value, and replace VLOOKUP with direct cell referencing to prevent crashes.
Spreadsheets store dates as sequential serial numbers, but VBA often interprets them as strings or standard Date types. This mismatch causes the MATCH function to fail.
By converting the date to a Long integer and utilizing the resulting row and column coordinates, you can extract the value directly from the range without relying on VLOOKUP.
In your VBA code, locate the MATCH function for the date. Wrap your date variable with the CLng() function. For example, use Application.WorksheetFunction.Match(CLng(retdate), dateRange, 0).
Use the WorksheetFunction.Match method to calculate both the row position (rowPos) for the date and the column position (colPos) for the fund name within your named range.
Instead of executing a VBA VLOOKUP, retrieve the final value using direct cell indexing. Assign your function return value using retTable.Cells(rowPos, colPos).Value.

Adjust Match Logic for Excel Tables (ListObjects)
If your data is formatted as an official Table rather than an ordinary Named Range, your VBA logic must target the ListObject properties instead of standard Range objects.
Easily Manage VBA Macros and Named Ranges in WPS Office
WPS Spreadsheet provides robust native VBA support, allowing you to seamlessly run, debug, and manage complex macros, MATCH functions, and lookups without modifying your existing scripts.
- 1. Open WPS Spreadsheet: Launch WPS Office and open your spreadsheet containing the named ranges and macro code.
- 2. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click on 'Visual Basic' to open the VBA editor.
- 3. Edit and Debug Code: Locate your module, apply the CLng() conversion to your date variables, and run the debugger to test the MATCH logic.
- 4. Save and Execute: Save your workbook as a macro-enabled file (.xlsm) and execute the function directly in your worksheet to verify the results.

Frequently Asked Questions
Why does the VBA MATCH function fail with dates?
In VBA, date variables are often treated as standard Date types or strings, whereas spreadsheet cells store dates as sequential serial numbers. Using the MATCH function without converting the VBA date to a long integer (using the CLng function) causes a mismatch, leading to failures.
Is it better to use VLOOKUP or Cells lookup in VBA?
While VLOOKUP works via Application.WorksheetFunction, using WorksheetFunction.Match to find row and column coordinates and then retrieving the value directly via Range.Cells(row, col).Value is generally faster, more flexible, and less prone to crashing in VBA scripts.
How do I know if my data is a Named Range or a Table?
You can check this in your spreadsheet by navigating to the Formulas tab and clicking 'Name Manager'. Alternatively, if you click inside the data and a 'Table Design' tab appears on the ribbon, it is an official Table (ListObject) rather than an ordinary named range.




