Excel2003-2007按单元格颜色求和的实现

上传人:公**** 文档编号:505327992 上传时间:2022-08-27 格式:DOCX 页数:5 大小:19.11KB
返回 下载 相关 举报
Excel2003-2007按单元格颜色求和的实现_第1页
第1页 / 共5页
Excel2003-2007按单元格颜色求和的实现_第2页
第2页 / 共5页
Excel2003-2007按单元格颜色求和的实现_第3页
第3页 / 共5页
Excel2003-2007按单元格颜色求和的实现_第4页
第4页 / 共5页
Excel2003-2007按单元格颜色求和的实现_第5页
第5页 / 共5页
亲,该文档总共5页,全部预览完了,如果喜欢就下载吧!
资源描述

《Excel2003-2007按单元格颜色求和的实现》由会员分享,可在线阅读,更多相关《Excel2003-2007按单元格颜色求和的实现(5页珍藏版)》请在金锄头文库上搜索。

1、精选优质文档-倾情为你奉上一在excel2003中按单元格背景颜色求和按颜色求和,在网上一查还真找到了这一方法,经过测试(在Excel2003和2007中),非常好用,整理出来,与大家分享:Excel 本身没有这个功能,可用以下办法实现:打开你的工作表:点工具-宏-Visual Basic 编辑器,在左边最上一行点右键-插入-模块,然后在打开的编辑框中粘贴以下代码:Function CountColor(col As Range, countrange As Range) As Integer Dim icell As Range Application.Volatile For Each i

2、cell In countrange If icell.Interior.ColorIndex = col.Interior.ColorIndex Then CountColor = CountColor + 1 End If Next icellEnd FunctionFunction SumColor(col As Range, sumrange As Range) As Integer Dim icell As Range Application.Volatile For Each icell In sumrange If icell.Interior.ColorIndex = col.

3、Interior.ColorIndex Then SumColor = Application.Sum(icell) + SumColor End If Next icellEnd Function然后关闭,反回到工作表,点击-工具-选项-安全性-宏安全性-安全级选-低-确定保存关闭工作表,然后再打开,这时就可以用了用法:按颜色求和:sumcolor(颜色示列格,求和区域或列);按颜色计数:countcolor(颜色示列格,求和区域或列)例如:要求和从a1到a10这个区域内的红色格,=sumcolor($a$1,$a$1:$A$10)计数:countcolor($a$1,$a$1:$A$10)

4、注意:$a$1 必须是红色格,这是定义颜色的,你也可以设成其它格,但必须是你要求和的颜色ok!有问题可以留言。二在excel2007中按单元格背景颜色求和在“开发工具”选项卡的“代码”组中,单击“宏安全性”。 提示如果未显示“开发工具”选项卡,请单击“Office 按钮” ,单击“Excel 选项”,然后在“常用”类别中的“使用 Excel 时采用的首选项”下单击“在功能区显示开发工具选项卡”。在“宏设置”类别中的“宏设置”下,单击所需的选项。 打开要进行分背景颜色计算的工作簿,点击开发工具-VISUALBASIC插入模块复制以下内容Function SumColor(col As Range

5、, sumrange As Range) As Integer Dim icell As Range Application.Volatile For Each icell In sumrange If icell.Interior.ColorIndex = col.Interior.ColorIndex Then SumColor = Application.Sum(icell) + SumColor End If Next icellEnd Function到打开的模块范围内,即会自动产生SUMCOLOR()函数;然后点击该VB的窗口保存图标-会显示“另存为”窗,在保存类型选项选择“exc

6、el启用宏的工作簿”,保存即可-在该工作簿中建立数据表格,在点击要放汇总的单元格公式f(x)函数选择类别-全部即会在下拉中看到sumcolor()函数-格式为sumcolor(操作颜色示例单元格例如A12,求和范围例B13:B78)-按出现的顺序操作即可。操作颜色示例单元格最好在图表旁边另建(颜色模板),防止因数据表中颜色调整而影响到计算结果;此处颜色不是字体颜色,而是背景颜色。建完后要进行保存,最好也保存为“excel启用宏的工作簿”。另外应将宏安全级别降低或改为提醒:开发工具宏安全-选择禁用所有宏,并发出通知或者启用所有宏(不推荐)关于countcolor()和sumcolor()的变更求

7、助按填充颜色或字体颜色求和及计数自定义函数语法:Countcolor(countrange,colorrange,fontorinterior)Sumcolor(countrange,colorrange,fontorinterior)countrange:要计算的单元格区域colorrange:要比较的单元格fontorinterior:为TRUE时,按内部填充颜色计算,为FALSE按字体颜色计算,默认为FALSE格式Sumcolor(countrange,colorrange,fontorinterior)与上面的顺序不同建好宏函数及表格后,在某单元格插入函数f(x),-全部-选中Sumc

8、olor,-按出现的窗口操作此例选项最上面为范围,中间为颜色示例,最下面为“填充色”及“字体色”的选择,填为true指按内部填充颜色计算,false为按字体颜色计算。格式为Sumcolor(countrange,colorrange,true)。别忘了保存注意事项!Function Countcolor(countrange As Range, colorrange As Range, Optional fontorinterior As Boolean = False)Dim iCell As RangeDim TF As BooleanApplication.VolatileFor Eac

9、h iCell In countrange If fontorinterior = True Then TF = iCell.Interior.colorindex = colorrange.Interior.colorindex Else TF = iCell.Font.colorindex = colorrange.Font.colorindex End If If TF Then Countcolor = Countcolor + 1 End IfNext iCellEnd FunctionFunction Sumcolor(sumrange As Range, colorrange A

10、s Range, Optional fontorinterior As Boolean = False)Dim iCell As RangeDim TF As BooleanApplication.VolatileFor Each iCell In sumrange If fontorinterior = True Then TF = iCell.Interior.colorindex = colorrange.Interior.colorindex Else TF = iCell.Font.colorindex = colorrange.Font.colorindex End If If TF Then Sumcolor = Sumcolor + Val(iCell) End IfNext iCellEnd Function注意:当完成所有工作并应用该功能时,会出现一个问题:直接修改字体颜色时,sumcolor单元格的数字会不变。解决办法:用格式刷来改变单元格的颜色,并以某固定格式单元格为范本单元格。专心-专注-专业

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 办公文档 > 教学/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号