#2383 - Fixed issue where VLOOKUP and HLOOKUP sometimes returned wrong value on unsorted data.#2384
Merged
Conversation
…g value on unsorted data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes
VLOOKUPandHLOOKUPwith approximate match (range_lookup= TRUE oromitted) 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
0or an incorrect row) on ranges containing blank cells — both sorteddata with gaps and unsorted data.
Fix
Approximate match now performs a plain binary search over the whole range, exactly
like Excel:
the midpoint sequence matches Excel's.
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 theempty remainder of the column — the lookup stays O(log n).
This is implemented in
LookupBinarySearch.SearchAscFullRange;VLookupandHLookupcall 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.