Skip to content

#2383 - Fixed issue where VLOOKUP and HLOOKUP sometimes returned wrong value on unsorted data.#2384

Merged
JanKallman merged 2 commits into
develop8from
bug/sc1054
Jun 11, 2026
Merged

#2383 - Fixed issue where VLOOKUP and HLOOKUP sometimes returned wrong value on unsorted data.#2384
JanKallman merged 2 commits into
develop8from
bug/sc1054

Conversation

@swmal

@swmal swmal commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

Summary

Fixes VLOOKUP and HLOOKUP with approximate match (range_lookup = TRUE or
omitted) returning results that differ from Excel, including the case reported in
issue 1054.

Problem

The approximate-match binary search did not run over the range as given. It first
trimmed the range to a value sub-range and removed all blank cells before searching.
This shifted the binary search midpoints relative to Excel, producing wrong results
(sometimes 0 or an incorrect row) on ranges containing blank cells — both sorted
data with gaps and unsorted data.

Fix

Approximate match now performs a plain binary search over the whole range, exactly
like Excel:

  • No trimming and no removal of cells. Blank cells keep their original positions, so
    the midpoint sequence matches Excel's.
  • A blank cell encountered at a midpoint is skipped forward to the next value to get a
    comparison direction (Excel sees past blanks). This scan is bounded by the position
    of the last value, so a whole-column reference (e.g. A:A) never scans across the
    empty remainder of the column — the lookup stays O(log n).
  • A non-comparable header in the first value position is handled as before.

This is implemented in LookupBinarySearch.SearchAscFullRange; VLookup and
HLookup call it for the approximate-match path.

Tests

Added coverage for approximate match with blank cells in leading, inner, and trailing
positions (vertical and horizontal), unsorted data, and a whole-column reference where
the data sits far above the empty tail.

Note

For unsorted data, approximate match is not standardised in Excel and the result
depends on the exact binary search path. This change makes EPPlus follow Excel's
search over the range, but exact parity on arbitrary unsorted data cannot be
guaranteed.

@swmal swmal added the bug Something isn't working label Jun 9, 2026
@JanKallman JanKallman merged commit 239f3cf into develop8 Jun 11, 2026
3 checks passed
@JanKallman JanKallman deleted the bug/sc1054 branch June 11, 2026 06:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants