Excel VBA sumifs sort and set

2019-09-15 10:14发布

I need some help in this query.

So I have this data:

|  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

With this data i need to calculate a sum based on unique dates and pozitive values.

For first unique_date i need:

  • sum of column B where the values > 0 and set in H2
  • sum of column C where the values > 0 and set in H3
  • sum of column D where the values > 0 and set in H4

For second unique_date i need:

  • sum of column B where the values > 0 and set in H5
  • sum of column C where the values > 0 and set in H6
  • sum of column D where the values > 0 and set in H7

For third unique_date i need:

  • sum of column B where the values > 0 and set in H8
  • sum of column C where the values > 0 and set in H9
  • sum of column D where the values > 0 and set in H10

For forth unique_date i need:

  • sum of column B where the values > 0 and set in H11
  • sum of column C where the values > 0 and set in H12
  • sum of column D where the values > 0 and set in H13

The column "F" with unique data from column "A" is work. But the column "H", still doesn't work.

The results i need is:

|   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

This is the code which i write:

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

---- The problem is somewhere bellow, because i don't know how to loop it

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

1条回答
欢心
2楼-- · 2019-09-15 10:30

edited after OP clarifications:

substitute:

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

with:

    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
查看更多
登录 后发表回答