简体   繁体   English

Excel公式到SUMIF日期特定于月份

[英]Excel Formula to SUMIF date falls in particular month

I have excel data in following format. 我有以下格式的excel数据。

Date        Amount

03-Jan-13   430.00 
25-Jan-13   96.00 
10-Jan-13   440.00 
28-Feb-13   72.10 
28-Feb-13   72.30

I need to sum the amount field only if the month lies in Jan Month. 我只需要在月份位于1月份时对金额字段求和。

What i have tried is , 我试过的是,

=SUMIF(A2:A6,"MONTH(A2:A6)=1",B2:B6)

But it returns, 但它回来了,

0

What i need is, 我需要的是,

Following values to be summed, 430.00 + 96.00 + 440.00 = 966.00

Try this instead: 试试这个:

  =SUM(IF(MONTH($A$2:$A$6)=1,$B$2:$B$6,0))

It's an array formula, so you will need to enter it with the Control - Shift - Enter key combination. 这是一个数组公式,因此您需要使用Control - Shift - Enter组合键输入它。

Here's how the formula works. 这是公式的工作原理。

  1. MONTH($A$2:$A$6) creates an array of numeric values of the month for the dates in A2:A6, that is, MONTH($ A $ 2:$ A $ 6)为A2中的日期创建一个月份数值数组:A6,即
    {1, 1, 1, 2, 2} . {1, 1, 1, 2, 2}
  2. Then the comparison {1, 1, 1, 2, 2}= 1 produces the array {TRUE, TRUE, TRUE, FALSE, FALSE} , which comprises the condition for the IF statement. 然后比较{1, 1, 1, 2, 2}= 1产生数组{TRUE, TRUE, TRUE, FALSE, FALSE} ,其包括IF语句的条件。
  3. The IF statement then returns an array of values, with {430, 96, 400.. for the values of the sum ranges where the month value equals 1 and ..0,0} where the month value does not equal 1. 然后,IF语句返回一个值数组,其中{430, 96, 400.. ..0,0} {430, 96, 400..表示月份值等于1和..0,0}的和范围的值,其中月份值不等于1。
  4. That array {430, 96, 400, 0, 0} is then summed to get the answer you are looking for. 然后将该数组{430, 96, 400, 0, 0}求和以得到您正在寻找的答案。

This is essentially equivalent to what the SUMIF and SUMIFs functions do. 这基本上等同于SUMIF和SUMIFs函数的功能。 However, neither of those functions support the kind of calculation you tried to include in the conditional. 但是,这些函数都不支持您尝试包含在条件中的计算类型。

It's also possible to drop the IF completely. 也可以完全放弃IF。 Since TRUE and FALSE can also be treated as 1 and 0, this formula-- =SUM((MONTH($A$2:$A$6)=1)*$B$2:$B$6) --also works. 由于TRUE和FALSE也可以被视为1和0,因此该公式 - =SUM((MONTH($A$2:$A$6)=1)*$B$2:$B$6)也可以。

Heads up: This does not work in Google Spreadsheets 抬头:这在Google Spreadsheets中不起作用

=Sumifs(B:B,A:A,">=1/1/2013",A:A,"<=1/31/2013")

这个公式的优点是你可以向A列和B列添加更多数据,它只会重新计算。

=SUMPRODUCT( (MONTH($A$2:$A$6)=1) * ($B$2:$B$6) )

Explanation: 说明:

  • (MONTH($A$2:$A$6)=1) creates an array of 1 and 0, it's 1 when the month is january, thus in your example the returned array would be [1, 1, 1, 0, 0] (MONTH($A$2:$A$6)=1)创建一个1和0的数组,当月份是1月时它是1,因此在你的例子中返回的数组将是[1, 1, 1, 0, 0]

  • SUMPRODUCT first multiplies each value of the array created in the above step with values of the array ($B$2:$B$6) , then it sums them. SUMPRODUCT首先将上一步中创建的数组的每个值与数组的值($B$2:$B$6)相乘,然后将它们相加。 Hence in your example it does this: (1 * 430) + (1 * 96) + (1 * 440) + (0 * 72.10) + (0 * 72.30) 因此,在您的示例中,它执行此操作: (1 * 430) + (1 * 96) + (1 * 440) + (0 * 72.10) + (0 * 72.30)

This works also in OpenOffice and Google Spreadsheets 这也适用于OpenOffice和Google Spreadsheets

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

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