ExcelVBA-自定义函数MultiConTosum用于多条件求和
函数 自定义 条件 用于 求和 ExcelVBA
2023-06-13 09:14:17 时间
我们在日常生活中常有这样的求和
多条件求和
我们常用函数是sumif,sumifs,sumproduct
我认为输入公式计算多条件求和时有点复杂,所以我想能不能搞个简单一点的
想了想,搞一个吧,练练手
'传入一维数组和标题,返回标题在数组中的位置
Function StrToId(inarr, s)
Dim t_m%
On Error Resume Next
t_m = Application.WorksheetFunction.Match(s, inarr, 0)
If Err = 0 Then
StrToId = t_m
Else
StrToId = 0
End If
On Error GoTo 0
End Function
'=MultiConTosum(数据全区域,条件标题区域,求和标题,条件区域)
'例:=MultiConTosum($A$1:$J$13,$B$21:$C$21,D$21,$B22:$C22)
'1.数据全区域----绝对引用区域第一列第一行开始(一定含标题行)
'2.条件标题区域--绝对引用条件标题
'3.求和标题------列绝对引用求和标题(一个单元格)
'4.条件区域------行绝对引用
Function MultiConTosum(dataRng As range, conTitleRng As range, sumRng As range, conRng As range)
Dim data_arr, data_arr1, con_arr, t_Array()
Dim t_num, y, k As Integer, sumStr As String, rr As range, gotoNext As Boolean, get_Col As Integer
Dim dic As Object
Set dic = CreateObject("scripting.dictionary")
data_arr = dataRng.Value
data_arr1 = Application.Index(data_arr, 1, 0)
con_arr = conRng.Value
t_num = Application.WorksheetFunction.CountA(conTitleRng)
k = 1
gotoNext = True
If t_num > 0 Then
ReDim t_Array(1 To t_num)
sjoin = Join(Application.Index(con_arr, 1, 0), "")
dic(sjoin) = 0
For Each rr In conTitleRng
If rr <> "" Then
t_Array(k) = StrToId(data_arr1, rr.Value)
k = k + 1
End If
Next
Else
gotoNext = False
End If
get_Col = StrToId(data_arr1, sumRng.Value)
If get_Col = 0 Then gotoNext = False
If gotoNext = False Then
MultiConTosum = 0
Else
For k = 2 To UBound(data_arr)
s = Join(Application.Index(data_arr, k, t_Array), "")
If dic.exists(s) Then dic(s) = dic(s) + data_arr(k, get_Col)
Next k
MultiConTosum = dic(sjoin)
End If
End Function
【效果】
相关文章
- 自定义的delay函数
- MySQL数据库:第五章:常见函数
- C/C++ Qt 信号自定义槽函数
- Python 函数文档字符串可以动态更新
- 第十四届蓝桥杯集训——自定义函数+static+return
- Linux | 内存管理之mmap函数
- 【Android Gradle 插件】Android 依赖管理 ③ ( dependencies 依赖配置 | Project#dependencies 函数分析 | 自定义依赖配置 )
- Django REST Framework-函数视图
- FunDA(13)- 示范:用户自定义操作函数 – user defined tasks详解编程语言
- 用Write函数写入Linux文件(write函数linux)
- MySQL自定义函数(CREATE FUNCTION)
- 优先使用函数对象自定义STL算法规则
- Python中用format函数格式化字符串的用法
- 如何在MySQL中修改函数(mysql中修改函数)
- 函数Oracle中单行和多行函数的应用技巧(oracle中单行多行)
- Oracle 中使用长度函数获取字符串长度的技巧(oracle中长度函数)
- javascript的trim,ltrim,rtrim自定义函数
- JS自定义函数缺省值的设置方法
- JS自定义带默认值的函数
- Oracle中instr函数使用方法
- php数组去重的函数代码
- string中c_str(),data(),copy(p,n)函数的用法总结
- JavaScript自定义日期格式化函数详细解析
- jquery中ajax函数执行顺序问题之如何设置同步
- Mysql中实现提取字符串中的数字的自定义函数分享