Excel:需要复杂的SUMPRODUCT公式

时间:2014-07-16 12:08:16

标签: excel excel-formula excel-2003

我正在寻找SUMPRODUCT公式,或类似的公式,它做同样的事情。它应该执行以下操作:

  1. 在工作表A上,它需要忽略不正确的zipcodes,这意味着 不需要包含4个数字和2个字母的邮政编码 忽略。它还需要考虑到有时存在 邮政编码背后的多余空间。有时会有一个 数字和字母之间的空格,有时候没有。只是 需要比较4个数字和2个字母。
  2. 工作表A上的正确邮政编码需要与之比较 工作表B上的zipcodes。如果匹配,那么后面的所有值 邮政编码需要总结。如果有另一条记录 从相同的邮政编码开始,然后这些需要加起来 好。
  3. 由于数据是经常生成的,因此不需要更改任何工作表。该公式应该能够在第三个单独的工作表上工作。它应该在Excel 2003中工作。
  4. 编辑:添加第3点。

    我会添加一张图片来形象化我的意思。希望有人可以帮助我!

    enter image description here

2 个答案:

答案 0 :(得分:3)

使用一些辅助列,您可以使用类似的东西(在新标签中打开以获得更大版本):

enter image description here

公式:

在B2中删除空格,从而获得“干净”的ZIP并检查长度:

=IF(LEN(SUBSTITUTE(A2," ",""))=6,SUBSTITUTE(A2," ",""),"")

在C2中,得到总和:

=IFERROR(IF(AND(ISNUMBER(LEFT(B2,4)*1),CODE(MID(LOWER(B2),5,1))>=97,CODE(MID(LOWER(B2),5,1))<=122,CODE(RIGHT(LOWER(B2)))>=97,CODE(RIGHT(LOWER(B2)))<=122),SUMPRODUCT($H$2:$K$8*($G$2:$G$8=B2)),""),"")

在G2中,我使用与B2中相同的一个:

=IF(LEN(SUBSTITUTE(F2," ",""))=6,SUBSTITUTE(F2," ",""),"")

如果没有助手,由于重复部分,公式会变得更长:

=IFERROR(IF(AND(LEN(SUBSTITUTE(A2," ",""))=6,ISNUMBER(LEFT(SUBSTITUTE(A2," ",""),4)*1),CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))>=97,CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))<=122,CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))>=97,CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))<=122),SUMPRODUCT($H$2:$K$8*(SUBSTITUTE($F$2:$F$8," ","")=SUBSTITUTE(A2," ",""))),""),"")

或者

=IFERROR(
    IF(
        AND(
            LEN(SUBSTITUTE(A2," ",""))=6,                      ' Check length
            ISNUMBER(LEFT(SUBSTITUTE(A2," ",""),4)*1),         ' Check numbers
            CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))>=97,   ' Check if letter
            CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))<=122,  ' Check if letter
            CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))>=97,     ' Check if letter
            CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))<=122     ' Check if letter
        ),
        SUMPRODUCT(
            $H$2:$K$8*
            (SUBSTITUTE($F$2:$F$8," ","")=SUBSTITUTE(A2," ",""))),
        ""
    ),
    ""
)

哎呀,忘记了IFERROR不在2003年。我使用它的唯一原因是MID会返回一个空字符串,CODE随后会出错。您可以使用下面的代码来确保字符串首先是6个字符:

=IF(LEN(SUBSTITUTE(A2," ",""))=6,IF(AND(ISNUMBER(LEFT(SUBSTITUTE(A2," ",""),4)*1),CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))>=97,CODE(MID(LOWER(SUBSTITUTE(A2," ","")),5,1))<=122,CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))>=97,CODE(RIGHT(LOWER(SUBSTITUTE(A2," ",""))))<=122),SUMPRODUCT($H$2:$K$8*(SUBSTITUTE($F$2:$F$8," ","")=SUBSTITUTE(A2," ",""))),""),"")

答案 1 :(得分:1)

这里有一个验证荷兰邮政编码的公式

=AND(LEN(A2)=6; ISNUMBER(VALUE(LEFT(A2;4))); CODE(MID(LOWER(A2);5;1)) >= 97; CODE(MID(LOWER(A2);5;1)) <= 122; CODE(MID(LOWER(A2);6;1)) >= 97; CODE(MID(LOWER(A2);6;1)) <= 122)

0-9 = ASCII码48到57
a-z = ASCII代码97到122(小写)

如果您有荷兰语版本的Excel,则公式为:

=EN(LENGTE(A2)=6; ISGETAL(WAARDE(LINKS(A2;4))); CODE(DEEL(KLEINE.LETTERS(A2);5;1)) >= 97; CODE(DEEL(KLEINE.LETTERS(A2);5;1)) <= 122; CODE(DEEL(KLEINE.LETTERS(A2);6;1)) >= 97; CODE(DEEL(KLEINE.LETTERS(A2);6;1)) <= 122)