如何用区间求和

how to sum with an interval

我正在寻找可以使用以下内容的公式或宏:我需要对每周的金额进行平方和。这应该从一开始就完成。我的数据结构如下:

Col A   Col B   Column C
Year    Week    Amount
2000    1       368
2000    2       8646
 …      …       …
2000    52      46846
2001    1       656
2001    2       846
 …      …       …
2001    52      4651
2002    1       489
 …      …       …
2014    52      46546

我会有一个 D 列,其中我有每周金额的平方和。所以 Cell(Column "D", "week 2000w1") 应该是,

=SUMSQ(Amount 2000w1)

第一年,这很容易。问题出现在明年。在单元格(列 "D",周“2001w1”)中,公式应为,

=SUMSQ(Amount 2000w1;Amount 2001w1)

对于去年,单元格(列 "D",周“2014w1”)应该是公式,

=SUMSQ(Amount 2000w1;Amount 2001w1; Amount 2002w1;Amount 2003w1;Amount 2004w1; Amount 2005w1;Amount 2006w1;Amount 2007w1; Amount 2008w1;Amount 2009w1;Amount 2010w1; Amount 2011w1;Amount 2012w1;Amount 2013w1)

所有年份的第 1 周到第 52 周都应该这样做。有快速的方法吗?

这是一个带有工作表功能的解决方案,您也可以使用宏进行类似的开发,但我认为现在没有它会更容易:

=SUMSQ(INDEX([Amount]*([Week]=[@Week])*([Year]<=[@Year]),0))

谢谢 Juhász Máté 偷了我的 jerb。现在我为开发这个而感到愚蠢。
无论如何,这是一个 VBA 解决方案。
对于您的示例数据集 =SUMQ($C:$C, $B:$B, $B2) 将给出 804,881 即 *week1*s 平方的总和。
=SUMQ($C:$C, $B:$B, $B2, $A:$A, "<=", $A2) 的高级用法将给出 135,424,即小于或等于 2000.

的 *week1*s 的总和
Public Function SUMQ(NumsToSquare As Range, Filter1 As Range, FilterCriterion As Variant, _
                     Optional Filter2 As Range, Optional FilterRelation As String, Optional FilterCriterion2 As Variant) As Long

Set NumsToSquare = Intersect(NumsToSquare, NumsToSquare.Worksheet.UsedRange)
Set Filter1 = Intersect(Filter1, Filter1.Worksheet.UsedRange)
RowsCount = Filter1.Rows.Count
ColumnsCount = Filter1.Columns.Count

If Not Filter2 Is Nothing Then Advanced = True
If Advanced Then Set Filter2 = Intersect(Filter2, Filter2.Worksheet.UsedRange)

On Error Resume Next
For i = 1 To RowsCount
    For j = 1 To ColumnsCount
        If Not Advanced Then
            If Filter1(i, j).Value2 = FilterCriterion Then SUMQ = SUMQ + NumsToSquare(i, j).Value2 ^ 2
        Else
            If Filter1(i, j).Value2 = FilterCriterion And Judge(Filter2(i, j).Value2, FilterRelation, FilterCriterion2) Then SUMQ = SUMQ + NumsToSquare(i, j).Value2 ^ 2
        End If
    Next j
Next i
End Function

Private Function Judge(var1 As Variant, FilterRelation As String, var2 As Variant) As Boolean
Judge = False
On Error GoTo err:
Select Case FilterRelation 'cf. https://msdn.microsoft.com/en-us/library/aa711633(v=vs.71).aspx
    Case "=" 'The = operator tests whether the two operands are equal.
        Judge = (var1 = var2)
    Case "<>" 'The <> operator tests whether the two operands are not equal.
        Judge = (var1 <> var2)
    Case "<" 'The < operator tests whether the first operand is less than the second operand.
        Judge = (var1 < var2)
    Case ">" 'The > operator tests whether the first operand is greater than the second operand.
        Judge = (var1 > var2)
    Case "<=" 'The <= operator tests whether the first operand is less than or equal to the second operand.
        Judge = (var1 <= var2)
    Case ">=" 'The >= operator tests whether the first operand is greater than or equal to the second operand.
        Judge = (var1 >= var2)
End Select
err:
End Function