索引匹配以返回最接近匹配值的相邻单元

时间:2013-09-04 17:08:28

标签: excel excel-vba excel-formula excel-2010 vba

这是我的2个表格的简化版本:

enter image description here

我需要能够根据相邻测量值与 Table1 中该值的最接近匹配来填充 Table2 中的缺失值。类型。

例如,在第18行,我需要查找单元格 B18 [4.3]与单元格 B3 B11 最接近的匹配(因为这两个是唯一的A类记录),并将最接近匹配的相邻 measurement2 返回到单元格 C18 。或者,在第19行中,我需要在单元格 C6:C8 中查找单元格 C19 [2.3]的最接近匹配,并返回相邻的 measurement1 进入细胞 B19

2 个答案:

答案 0 :(得分:3)

我会稍微设置表2,如下所示:

Index Match Closest Value

单元格D18中的数组公式并向下复制:

=INDEX(IF(B18=$B$2,$C$3:$C$12,$B$3:$B$12),MATCH(MIN(IF($A$3:$A$12=A18,ABS(IF(B18=$B$2,$B$3:$B$12,$C$3:$C$12)-C18))),IF($A$3:$A$12=A18,ABS(IF(B18=$B$2,$B$3:$B$12,$C$3:$C$12)-C18)),0))

为了确认数组公式,您需要使用 Ctrl + Shift + Enter 而不是Enter。

答案 1 :(得分:2)

好的,我不知道它是否可以进一步简化(公式中有两次计算),但我想出了C18单元格:

=INDEX(C$3:C$12,MATCH(9^99,IF(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))),C$3:C$12)))

这对于细胞B19:

=INDEX(B$3:B$12,MATCH(9^99,IF(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19))=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),B$3:B$12)))

EDIT2:正如barryhoudini所指出的那样,他的公式出现了错误。我编辑了以上两个公式。否则,正如他正确提到的那样,您也可以将此公式用于C18:

=LOOKUP(9^99,IF(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))=MIN(IF($A$3:$A$12=$A18‌​,ABS($B$3:$B$12-$B18))),C$3:C$12))

注意:

  1. 这些是数组公式,仅适用于 Ctrl + Shift + 输入而不是 Enter

  2. 公式将获得表中的最后一个匹配,在本例中,对于A,其中measurement1为4.3,如果存在相同的差异,则返回2.8。

  3. EDIT1:好的,为了保持一致性,我想出了这个,以便在B19中获得更高的匹配:

    EDIT3:并使用LOOKUP而不是INDEX / MATCH来缩短公式,并修复了第一栏问题。

    =LOOKUP(9^99,IF(MAX(IF(ABS($C$3:$C$12-$C19)=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),C$3:C$12))=IF(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19))=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),C$3:C$12),IF(ABS($C$3:$C$12-$C19)=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),B$3:B$12)))
    

    这是C18:

    =LOOKUP(9^99,IF(MAX(IF(ABS($B$3:$B$12-$B18)=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),B$3:B$12))=IF(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),B$3:B$12),IF(ABS($B$3:$B$12-$B18)=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),C$3:C$12)))
    

    对于较低的比赛,B19:

    =LOOKUP(9^99,IF(MIN(IF(ABS($C$3:$C$12-$C19)=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),C$3:C$12))=IF(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19))=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),C$3:C$12),IF(ABS($C$3:$C$12-$C19)=MIN(IF($A$3:$A$12=$A19,ABS($C$3:$C$12-$C19),999)),B$3:B$12)))
    

    对于C18:

    =LOOKUP(9^99,IF(MIN(IF(ABS($B$3:$B$12-$B18)=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),B$3:B$12))=IF(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18))=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),B$3:B$12),IF(ABS($B$3:$B$12-$B18)=MIN(IF($A$3:$A$12=$A18,ABS($B$3:$B$12-$B18),999)),C$3:C$12)))
    

    <强> EDIT4: 有两种类型:

    =LOOKUP(9^99,IF(MAX(IF(ABS($C$3:$C$12-$C19)=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),C$3:C$12))=IF(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19))=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),C$3:C$12),IF(ABS($C$3:$C$12-$C19)=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),B$3:B$12)))
    

    这是C18:

    =LOOKUP(9^99,IF(MAX(IF(ABS($B$3:$B$12-$B18)=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),B$3:B$12))=IF(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18))=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),B$3:B$12),IF(ABS($B$3:$B$12-$B18)=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),C$3:C$12)))
    

    对于较低的比赛,B19:

    =LOOKUP(9^99,IF(MIN(IF(ABS($C$3:$C$12-$C19)=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),C$3:C$12))=IF(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19))=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),C$3:C$12),IF(ABS($C$3:$C$12-$C19)=MIN(IF(($A$3:$A$12=$A19)*($D$3:$D$12=$D19),ABS($C$3:$C$12-$C19),999)),B$3:B$12)))
    

    对于C18:

    =LOOKUP(9^99,IF(MIN(IF(ABS($B$3:$B$12-$B18)=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),B$3:B$12))=IF(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18))=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),B$3:B$12),IF(ABS($B$3:$B$12-$B18)=MIN(IF(($A$3:$A$12=$A18)*($D$3:$D$12=$D18),ABS($B$3:$B$12-$B18),999)),C$3:C$12)))