vba - 仅使用 VBA 选择可见单元格的范围
问题描述
我想在状态跟踪器上使用以下函数“CountCcolor()”。我的目的是能够使用该函数在可见单元格的单个列范围中查找有多少以特定颜色突出显示,比如绿色。
Function CountCcolor(range_data As Range, criteria As Range) As Long
Dim datax As Range
Dim xcolor As Long
' The next one-liner does not work. Without it, it selects visible and hidden cells. I only want it to select visible cells:
range_data = Selection.SpecialCells(xlCellTypeVisible).Select
xcolor = criteria.Interior.ColorIndex
For Each datax In range_data
If datax.Interior.ColorIndex = xcolor Then
CountCcolor = CountCcolor + 1
End If
Next datax
End Function
提前谢谢你的帮助!
解决方案
不要使用Interior.ColorIndex
相反,只需使用Interior.Color
. 我也被这一次难住了。简而言之,ColorIndex
代表的是一种颜色,而不是一种独特的颜色。请参阅此处了解更多详细信息
Function CountCcolor(range_data As Range, criteria as Range) As Long
Dim myRange As Range, myCell As Range, TempCount As Long
Set myRange = range_data.SpecialCells(xlCellTypeVisible)
For Each myCell In myRange
If myCell.Interior.Color = criteria.Interior.Color Then
TempCount = TempCount + 1
End If
Next myCell
CountCcolor = TempCount
End Function
推荐阅读
- firebase - angularfire从firebase获得恒定值
- sql-server-2014 - 如何创建解析字段的查询
- laravel - Laravel 路由多层深层子目录
- r - R 令牌创建过程中的 Google Analytics
- javascript - node.js 的繁琐模块是否有任何防止 sql 注入的功能?
- python - matplob 动画情节散点图
- ruby - 在 csv 中查找特定行并使用 Ruby 为找到的行更新 csv 中的某些值
- sql - 对连接字段执行 COUNT DISTINCT 并限制为 >= 2
- javascript - 如何加载 JSON 文件,然后在选择下拉菜单中填充数据?
- mysql - 我需要删除最后一个整数之后的所有内容