如何与间隔相加

时间:2015-07-10 08:50:45

标签: excel intervals

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

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(专栏#34; D","周2000w1")应该是,

=SUMSQ(Amount 2000w1)

第一年,这很容易。问题发生在明年。在Cell(Column" D",week" 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周进行。有快速的方法吗?

2 个答案:

答案 0 :(得分:1)

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

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

enter image description here

答案 1 :(得分:1)

感谢JuhászMá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