2015-07-10 73 views
0

我在尋找一個公式或宏可以使用以下內容:我需要每週的總和平方數。這應該從一開始就完成。我的數據結構如下:如何與間隔相加

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列,其中我有每週數量的平方和。所以細胞(列「D」,「周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周完成。有沒有快速的方法來做到這一點?

+0

'SUMQ'是一個自定義函數,還是'SUMSQ'中的拼寫錯誤?你對VBA解決方案感興趣嗎?從你的暱稱判斷,你是... – user3819867

+0

嗨,的確我犯了一個錯字。它應該是SUMSQ。由於我正在與Excel合作,我總是希望通過開發一個宏來獲得時間。但是因爲我是新手,所以我不瞭解每個VBA解決方案,而我應該能夠理解任何功能。所以兩種解決方案都非常受歡迎 –

回答

1

這是worksheetfunction的解決方案,你可以開發與宏太相似了,但現在我認爲這是不容易:

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

enter image description here

+0

如何限制2005年以後的數據功能? –

+0

'* [Year]> = 2005'每個新的布爾語句都需要爲'TRUE'(除非添加)。對於eaxample,如果您尋找的是比2005年高或低於2001年的27周或更長時間,那麼= = SUMSQ(INDEX([Amount] *([Week]> = 27)*(([Year] <2001)+( [Year]> 2005)),0))'將成爲你的公式。 – user3819867

1

謝謝Juhász隊友偷了我jerb。現在我覺得這個發展很愚蠢。
無論如何,這是一個VBA解決方案。
對於您的樣本數據集=SUMQ($C:$C, $B:$B, $B2)將給出804,881即* week1 * s平方的總和。
作爲=SUMQ($C:$C, $B:$B, $B2, $A:$A, "<=", $A2)的高級使用將給出135,424,該年數爲低於或等於2000年*周1 * 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