2016-08-22 86 views
0

我需要一些幫助,在這個查詢。Excel VBA sumifs排序和設置

所以我有這樣的數據:

| A | B | C | D | 
---------------------------------- 
Date  int001 int002 int003 
2/1/2016  10  11  12 
2/2/2016  5  5  -5 
2/3/2016 -4  -4  4 
2/4/2016  5  5  -5 
2/1/2016  2  -2  2 
2/2/2016 -3  -2  -1 
2/3/2016  1  1  1 
2/4/2016  2  -1  3 
2/1/2016 -4  -5  6 
2/2/2016 -2  -2  2 
2/3/2016 -1  1  -1 
2/4/2016 -5  -3  1 

有了這些數據,我需要基於獨特的日期和pozitive值來計算的總和。

對於第一unique_date我需要:塔B的

  • 總和其中值> 0,在H2設置
  • 總和塔C的其中的值> 0並且在H3設定的
  • 總和列d其中值> 0,在H4

對於第二unique_date設置我需要:

    塔B的
  • 總和其中值> 0並且在H5設定
  • 總和塔C的其中的值> 0並且在H6設置
  • 柱d的
  • 總和其中值> 0並且在H7
設置

對於第三unique_date我需要:塔B的

  • 總和其中值> 0並且在H8設定
  • 總和塔C的其中的值> 0並且在H9
  • 設置
  • 總和柱d的其中的值> 0並且在H10

設定對於所述unique_date我需要:塔B的

  • 總和其中值> 0,在H11設置
  • 總和列C的其中的值> 0並且在H12設置
  • 柱d的總和
  • 其中具有uniqu的值> 0並且在H13

列「F」設置來自列「A」的數據是工作。 但列「H」,仍然不起作用。

我需要的結果是:

| F  | H | 
--------------------- 
Uniq Dates Results: 
2/1/2016  12 
2/2/2016  11 
2/3/2016  20 
2/4/2016  5 
      5 
      2 
      1 
      2 
      5 
      7 
      5 
      4 

這是我寫的代碼:

Sub Tsum() 

Dim int001 As Range 
Dim int002 As Range 
Dim int003 As Range 
Dim data1 As Date 
Dim data2 As Date 
Dim data3 As Date 
Dim nr_rows As Integer 
Dim dates As Range 
Dim nr_unique_dates As Integer 
Dim unique_dates As Range 

'-------------------------------------------------------------- 
nr_rows = Cells(Rows.Count, "A").End(xlUp).Row 
Set int001 = Range("B2:B" & nr_rows) 
Set int002 = Range("C2:C" & nr_rows) 
Set int003 = Range("D2:D" & nr_rows) 
Set dates = Range("A2:A" & nr_rows) 
'-------------------------------------------------------------- 
Range("A1:A" & nr_rows).AdvancedFilter Action:=xlFilterCopy,   CopyToRange:=Range("F1"), Unique:=True 
nr_unique_dates = Cells(Rows.Count, "F").End(xlUp).Row 
Set unique_dates = Range("F2:F" & nr_unique_dates) 

Range("H2:H" & nr_rows) = Application.WorksheetFunction.SumIfs(dates, int001, int002, int003, unique_dates, int001, int002, int003, ">0") 

End Sub 

----問題是波紋管的地方,因爲我不知道該怎麼循環它

Range("H2:H" & nr_rows) = Application.WorksheetFunction.SumIfs(dates, int001, int002, int003, unique_dates, int001, int002, int003, ">0") 

回答

1

編輯 OP澄清後:

替代:

Range("H2:H" & nr_rows) = Application.WorksheetFunction.SumIfs(dates, int001, int002, int003, unique_dates, int001, int002, int003, ">0") 

有:

Dim iDate As Long 
    With Range("H1") 
     For iDate = 1 To nr_unique_dates - 1 
     .Offset((iDate - 1) * 3 + 1) = Application.WorksheetFunction.SumIfs(int001, dates, unique_dates(iDate), int001, ">0") 
     .Offset((iDate - 1) * 3 + 2) = Application.WorksheetFunction.SumIfs(int002, dates, unique_dates(iDate), int002, ">0") 
     .Offset((iDate - 1) * 3 + 3) = Application.WorksheetFunction.SumIfs(int003, dates, unique_dates(iDate), int003, ">0") 
     Next iDate 
    End With 
+0

謝謝你,對不起,我最初的解釋。我用正確的信息編輯帖子。你的代碼使列(B + C + D)的總和,我想分開。你能幫助我嗎? – BOB

+0

看到編輯答案。如果我完成了您的問題,請將其標記爲已接受。謝謝 – user3598756

+0

當我用int001,int002和int003的值引入另一個日期(2/4/2016)時,結果移位了1個空格,至少爲空或0.我該如何解決這個問題? – BOB