网站首页 > 技术文章 正文
工作中你可能会遇到这样一个问题。
对相同颜色的单元格内容进行求和或者计数。
本篇分享两种方法给大家,一种函数法,一种VBA法。
第一种:函数法
利用GET.CELL函数获取单元格格式内容,然后使用sumifs,countifs函数计算出需要的结果。这种方式需要增加辅助列,且需要先点击C2单元格后设置。(因为我要在C2显示结果)
GET.CELL为早期版本函数,目前使用的版本都较高,所以需要通过名称管理器自定义。
GET.CELL(Type_num,Reference)中Type_num 1-66代表不同的含义,63代表单元格的填充(背景)颜色。
第二种:VBA法
通过编辑自定义函数,可以不加辅助的快速计算出所需结果。
通过循环For Each rg In countrange,不断判断单元格颜色是否与当前颜色一致来加以求和或者计数。
需要注意,自定义函数只能放置在模块内才可以有效。
Function Count颜色(countrange As Range, col As Range)其中countrangeAs Range为需计算区域,col As Range为目标颜色单元格。
具体代码如下:
Function Count颜色(countrange As Range, col As Range) As Integer
Dim rg As Range
Application.Volatile
For Each rg In countrange
If rg.Interior.ColorIndex = col.Interior.ColorIndex Then
Count颜色 = Count颜色 + 1
End If
Next
End Function
Function Sum颜色(sumrange As Range, col As Range) As Integer
Dim rg As Range
Application.Volatile
For Each rg In sumrange
If rg.Interior.ColorIndex = col.Interior.ColorIndex Then
Sum颜色 = Application.Sum(rg) + Sum颜色
End If
Next
End Function
欢迎点击关注,后续会有更多精彩内容!
猜你喜欢
- 2024-10-02 掌控你的MySQL语句执行方案(如何让mysql执行脚本?)
- 2024-10-02 我爱Julia之入门-075(字符串05)
- 2024-10-02 Java入门超经典教程-数组的操作(java数组视频教学)
- 2024-10-02 AdaBoost算法(手稿+代码)(adaboost算法详解)
- 2024-10-02 有了for循环 为什么还要forEach?(为什么用for循环)
- 2024-10-02 UFS深入浅出 第二章 UFS结构 第三节 UFS分区
- 2024-10-02 Ruby 最常用指令和函数(备忘查询)
- 2024-10-02 MYSQL优化有理有据全分析(面试必备)
- 2024-10-02 最小优先队列 Index min priority queue
- 2024-10-02 mysql explain用法(mysql游标的定义与使用)
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)