简体   繁体   English

Excels INDEX MATCH - 查找多个匹配项

[英]Excels INDEX MATCH - Finding multiple matches

------A ----------------- B ----------------------C ------------------------D  
1 --First--------------Last-----------------Start Date--------------End Date  
2 --John--------------Smith--------------08/08/2014------------01/01/2015  
3---John--------------Smith--------------08/11/2014------------17/11/2014  
4---John--------------Smith--------------06/06/2014------------23/12/2014  
5---Abel--------------Jones--------------14/05/2014------------29/04/2015  
6---Abel--------------Jones--------------04/07/2014------------26/04/2015

Sometimes on my spread sheet I get duplicate names. 有时在我的电子表格上我得到重复的名字。 For example the table above (with random data) there are 3 John's and 2 Abel's. 例如上面的表(随机数据)有3个John和2个Abel。 With these names is a start and end date. 这些名称是开始日期和结束日期。 If I have a start or end date which is earlier or later than the previous entry for that name, I would calculate the longest range (John Smith) by doing 如果我的开始日期或结束日期早于或晚于该名称的上一个条目,我会通过执行来计算最长范围(John Smith)

=MAX($D2:$D4)-MIN($C2:$C4)

This would give 209 days. 这将给209天。
Also, I want the formula to automatically recognize if a name has a duplicate within the range, and if so retrieve the date. 此外,我希望公式自动识别名称是否在范围内有重复,如果是,则检索日期。 In order to do this, I perform an INDEX MATCH function as follows. 为此,我按如下方式执行INDEX MATCH功能。

    =INDEX(C:C,
MATCH(1,INDEX((A:A=$A3)*(B:B=$B3),0),0))

This should give 08/08/2014. 这应该是08/08/2014。
So then I combine the two formulas together to perform a search for first name and last name comparing the start and end date of the matches, to find the longest possible date range. 然后,我将两个公式组合在一起,搜索比较匹配开始日期和结束日期的名字和姓氏,以找到最长的日期范围。

=MAX(
   (INDEX($C:$C,
       MATCH(1,INDEX(($A:$A=$A4)*($B:$B=$B4),0),0)))
:$D4)
-
MIN(
   (INDEX($D:$D,
       MATCH(1,INDEX(($A:$A=$A4)*($B:$B=$B4),0),0)))
:$C4)

Again this gives 209. 这又给出了209。
The problem which I am now having with this formula, is that when the INDEX MATCH function scans through the list of names to find a duplicate, if it does, it will only match against the first. 我现在使用这个公式的问题是,当INDEX MATCH函数扫描名称列表以找到重复时,如果是,它将只匹配第一个。 If there is more than one duplicate, it will ignore it. 如果有多个副本,它将忽略它。 There are 3 John Smiths in the table, I want the formula to check for the longest range between all duplicates. 表中有3个John Smiths,我希望公式检查所有重复项之间的最长范围。 Does this make sense and is this possible? 这是否有意义,这可能吗?

I would define the ranges more surgically when using array formulas. 在使用数组公式时,我会更加手术地定义范围。 Given the small data set, this array formula gives the expected result: 给定小数据集,此数组公式给出了预期结果:

=MAX(IF(INDEX($A$2:$A$6&" "&$B$2:$B$6, 0) = A2&" "&B2, $D$2:$D$6)) - MIN(IF(INDEX($A$2:$A$6&" "&$B$2:$B$6, 0) = A2&" "&B2, $C$2:$C$6))

...confirmed with Control+Shift+Enter to activate the array. ...通过Control + Shift + Enter确认激活阵列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM