当前位置:网站首页>Excel VBA: summarize calculation output results by date (SUMIF)

Excel VBA: summarize calculation output results by date (SUMIF)

2022-07-26 05:03:00 Avasla

Problem scenario

1) By date C Amount listed Cumulative Summary
2) By date and " spending / income " Summarize daily amount

PS: Direct output , You don't need a formula
![ Insert picture description here ](https://img-blog.csdnimg.cn/7a7844a59ee34fccb490ad6f123ecea8.p

Parameters are defined & Function description

Parameters are defined :

  • Range Format definition summary range
  • Long Format definition parameters . Because the length of subsequent parameters will exceed 32767, So choose Long.
    • Long( Long integer ) Variables are stored as signed 32 position (4 byte ) Numbers , Values range from -2,147,483,648 To 2,147,483,647.
    • Integer( plastic )Integer Variables are stored as 16 position (2 byte ) Numbers , The value range is -32,768 to 32,767.

Data format reference

Function USES :

  • WorksheetFunction.Round (expression, [ numdecimalplaces ])
  • WorksheetFunction.SumIfs (Arg1、Arg2、Arg3…)

Function reference

1) Result code : Daily cumulative summary amount

Sub  Daily cumulative summary amount ()

    Application.ScreenUpdating = False
    
    Dim sumRange As Range
    Dim criteriaRange_date As Range
    Dim e As Long, f As Long, g As Long

     ' Definition Range
    Set sumRange = Sheets("Sheet1").Range("C2:C31") ' amount of money 
    Set criteriaRange_date = Sheets("Sheet1").Range("A2:A31") ' date 
    
    Sheets("Sheet1").Select
    e = Range("K1048573").End(xlUp).Row '  Initial number of rows  - 1
    f = Range("J1048573").End(xlUp).Row ' Number of end lines 
    
    ' to update 
    For g = e + 1 To f
            ' Cumulative 
            Sheets("Sheet1").Cells(g, 11) = WorksheetFunction.Round(WorksheetFunction.SumIfs(sumRange, criteriaRange_date, "<=" & Cells(g, 10)), 2)
       Next g
    
End Sub

2) Result code : Daily expenditure income amount

Sub  Daily expenditure income amount ()

    Application.ScreenUpdating = False
    
    Dim sumRange As Range
    Dim criteriaRange As Range
    Dim criteriaRange_date As Range
    Dim e As Long, f As Long, g As Long

     ' Definition Range
    Set sumRange = Sheets("Sheet1").Range("C2:C31") ' amount of money 
    Set criteriaRange = Sheets("Sheet1").Range("D2:D31") ' classification 
    Set criteriaRange_date = Sheets("Sheet1").Range("A2:A31") ' date 
    
    Sheets("Sheet1").Select
    e = Range("O1048573").End(xlUp).Row '  Initial number of rows  - 1
    f = Range("N1048573").End(xlUp).Row ' Number of end lines 
    
    ' to update 
    For g = e + 1 To f
            ' Cumulative 
            Sheets("Sheet1").Cells(g, 15) = WorksheetFunction.Round(WorksheetFunction.SumIfs(sumRange, criteriaRange, Cells(g, 14), criteriaRange_date, "=" & Cells(g, 13)), 2)
       Next g
    
End Sub

Results after operation :

 Insert picture description here

原网站

版权声明
本文为[Avasla]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/207/202207260457266765.html