2016-08-22 89 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