当前位置:网站首页>Excel VBA:按日期汇总计算输出结果(sumif)
Excel VBA:按日期汇总计算输出结果(sumif)
2022-07-26 04:57:00 【Avasla】
问题场景
1)按日期将C列的金额累计汇总
2)按日期和"支出/收入"汇总每日金额
PS:直接输出结果,不需要公式
参数定义&函数说明
参数定义:
- Range格式定义汇总范围
- Long 格式定义参数。因为后续参数长度会超过32767,所以选取Long。
- Long(长整型)变量存储为带符号的 32 位(4 字节)数字,值范围从 -2,147,483,648 到 2,147,483,647。
- Integer(整形)Integer 变量存储为 16 位(2 字节)数字,值范围为 -32,768 至 32,767。
函数使用:
- WorksheetFunction.Round (expression, [ numdecimalplaces ])
- WorksheetFunction.SumIfs (Arg1、Arg2、Arg3…)
1)结果代码:每日累计汇总金额
Sub 每日累计汇总金额()
Application.ScreenUpdating = False
Dim sumRange As Range
Dim criteriaRange_date As Range
Dim e As Long, f As Long, g As Long
'定义Range
Set sumRange = Sheets("Sheet1").Range("C2:C31") '金额
Set criteriaRange_date = Sheets("Sheet1").Range("A2:A31") '日期
Sheets("Sheet1").Select
e = Range("K1048573").End(xlUp).Row ' 初始行数 - 1
f = Range("J1048573").End(xlUp).Row '结束行数
'更新
For g = e + 1 To f
'累计
Sheets("Sheet1").Cells(g, 11) = WorksheetFunction.Round(WorksheetFunction.SumIfs(sumRange, criteriaRange_date, "<=" & Cells(g, 10)), 2)
Next g
End Sub
2)结果代码:每日支出收入金额
Sub 每日支出收入金额()
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
'定义Range
Set sumRange = Sheets("Sheet1").Range("C2:C31") '金额
Set criteriaRange = Sheets("Sheet1").Range("D2:D31") '分类
Set criteriaRange_date = Sheets("Sheet1").Range("A2:A31") '日期
Sheets("Sheet1").Select
e = Range("O1048573").End(xlUp).Row ' 初始行数 - 1
f = Range("N1048573").End(xlUp).Row '结束行数
'更新
For g = e + 1 To f
'累计
Sheets("Sheet1").Cells(g, 15) = WorksheetFunction.Round(WorksheetFunction.SumIfs(sumRange, criteriaRange, Cells(g, 14), criteriaRange_date, "=" & Cells(g, 13)), 2)
Next g
End Sub
运行后结果:

边栏推荐
- 11、 Exception handler
- Molecular skeleton transition tool -delinker introduction
- 嵌入式实操----基于RT1170 FreeRTOS实现CPU使用率统计(二十四)
- 2022河南萌新联赛第(三)场:河南大学 J - 神奇数字
- 分子骨架跃迁工具-DeLinker介绍
- [mathematical modeling] analytic hierarchy process (AHP)
- [300 + selected interview questions from big companies continued to share] big data operation and maintenance sharp knife interview question column (VIII)
- 五、域对象共享数据
- Ffmpeg video coding
- Throttling anti shake function of JS handwritten function
猜你喜欢

Yapi installation

UE4 controls the rotation of objects by pressing keys

An SQL server queries the latest records as of a certain date

分子骨架跃迁工具-DeLinker介绍

C语言——指针一点通※

columns in GROUP BY clause; this is incompatible with sql_ mode=only_ full_ group_ By mysql8.0 solution

Interprocess communication

What is the real HTAP? (1) Background article
![[semantic segmentation] 2018-deeplabv3+ ECCV](/img/c9/d1e2d7e63df8db2a7fa2bde31b10f7.png)
[semantic segmentation] 2018-deeplabv3+ ECCV

To study the trend of open source and gain insight into the future of the industry, stonedb community and the China Academy of communications and communications released the Research Report on the dev
随机推荐
Array sort 1
Soft exam review and plan
"Game engine light in light out" 4. shader
补位,稍后补上
SQL加解密注入详解
Why is mongodb fast
C language -- string function, memory function collection and Simulation Implementation
can 串口 can 232 can 485 串口转CANbus总线网关模块CAN232/485MB转换器CANCOM
Working principle and application of fast recovery diode
Molecular skeleton transition tool -delinker introduction
The first open source MySQL native HTAP database in China will be released soon! Look at the three highlights first, limited to the surrounding areas, waiting for you~
data warehouse
3、 @requestmapping annotation
有ggjj看看这个问题没,是否缓存导致跨域问题?
Ggjj, do you have a look at this problem? Does caching cause cross domain problems?
IEC61131 数据类型与 C#数据类型的对应
Is this my vs not connected to the database
1、 Basic introduction
Database startup message: ora-29702: error occurred in cluster group service
Throttling anti shake function of JS handwritten function