excel - Excel VBA - 如果区域中的任何单元格文本被着色,则单元格中的颜色文本
问题描述
我制作了一个计算范围内红色文本单元格的函数。
Function CountRed(rngToSearch) As Long
Dim rngCel As Range
For Each rngCel In rngToSearch
If rngCel.Font.Colorindex = 3 Then
CountRed = CountRed + 1
End If
Next rngCel
End Function
我现在正在尝试编写另一个函数来对某个范围内的单元格求和,如果该范围内有任何单元格的文本颜色为红色,那么结果也应该是红色的。
这是我得到的:
Function SumR(rngToSum) As Long
Dim rngCel As Range
Dim IsThereRed
IsThereRed = 0
For Each rngCel In rngToSum
If rngCel.Font.Colorindex = 3 Then
IsThereRed = 1
End If
Next rngCel
SumR = Application.WorksheetFunction.Sum(Range(rngToSum))
If IsThereRed = 1 Then SumR.Font.Colorindex = 3
End Function
但我得到一个错误。有没有办法可以使用函数?
解决方案
要分配字体颜色,您需要定义一个范围来执行此操作。在这种情况下,您可以定义一个范围,您的结果 (SumR) 将显示在工作表中。
假设您的结果将在单元格 A10 中。您首先将结果放在该单元格中,然后根据需要为字体着色:
With Range("A10")
.Value2 = SumR
.Font.Color = RGB(255,0,0)
.NumberFormat = "#,##0" 'if you want to have a specific format for the number
End With
推荐阅读
- python-3.x - 今天的日期没有时间方面
- sql - 上传和读取数据集 SAS - SQL Server
- python - 在随机 Python 猜谜游戏中交换和删除数字
- c++ - 有没有办法计算 QStandardItem 的高度?
- c# - OpenXML - 文字处理 - 表格单元格命令
- python - 订阅者没有收到消息,Pyzmq
- sql - 我在数据透视表中有一个值“标记”,如何使用查询生成器根据“标记”值查找学生排名?
- ios - 遵循文档后,React Native IOS Xcode 发布版本打开 Metro 服务器
- angular - Word Office Js 更新文档Word,压缩并检查
- python - Numpy:连接不同的数据类型,同时保留每个数据类型