Excel 在具有重复键的稀疏范围内匹配错误
TheRange
中的数据为 {1,"",1,"",1,"",1,"",2}
。
=Match(2, TheRange, 1)
按预期返回9
。=Match(1.5, TheRange, 1)
按预期返回7
。=Match(1, TheRange, 1)
返回5
,这不是预期的。
有人遇到过这个吗?有人有解决办法吗?
此外,如果我在 VBA 中使用 Worksheet.Function.Match
,我会得到更多意想不到的结果。
The data in TheRange
is {1,"",1,"",1,"",1,"",2}
.
=Match(2, TheRange, 1)
returns9
as expected.=Match(1.5, TheRange, 1)
returns7
as expected.=Match(1, TheRange, 1)
returns5
which is not expected.
Has anyone come across this ? Does anyone have a fix?
Additionally, if I use Worksheet.Function.Match
in VBA, I get more unexpected results.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果为“match_type”参数(MATCH 的第三个参数)指定 1,则 Excel 期望对数组进行排序。显然,Excel 对值进行二分搜索 - 从中间开始,在搜索 1 时找到中间值(在您的情况下是第 5 个值)。
如果您为“match_type”指定 0,您将得到您所期望的结果- 至少对于这种情况。有关详细信息,请参阅 MATCH 函数的文档。
If you specify 1 for the "match_type" argument (the 3rd argument to MATCH), then Excel expects the array to be sorted. Apparently, Excel does a binary search for the value - starting in the middle and finding the middle value (which is the 5th value in your case) when searching for 1.
If you specify 0 for "match_type", you will get what you expect - at least for this case. See the documentation for the MATCH function for more info.
解决此问题的一种方法是将空白变为错误。
尝试这些公式:
这些是数组公式,因此当您输入它们时,必须使用“ctrl+shift+enter”确认它们,而不仅仅是“enter”。
One way to solve this is turn the blanks to errors.
Try these formula:
These are array formulas so when you type them in you must confirm them with "ctrl+shift+enter" instead of just "enter".