You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VBA统计黑色单元格函数偶尔计数异常问题求助

VBA统计黑色单元格函数偶尔计数异常问题求助

各位大佬好,我最近在做KPI表单的提交率统计,需求是计算已提交分数的占比,要排除空单元格和被涂黑的单元格。我先是借鉴了别人的代码然后做了调整,现在遇到一个诡异的问题:CountBlack函数有时候会莫名其妙多统计2个黑色单元格,实在找不到原因,想请大家帮忙看看!

我的需求逻辑是这样的:

  • 用CountBlack统计涂黑的单元格数量
  • 用CountAll统计选中区域的总单元格数
  • 再结合COUNTA()计算有效单元格的占比

但现在CountBlack偶尔会多算2个,下面是我的代码:

Function CountBlack(range_data As Range) As Long
    Dim datax As Range
    CountBlack = 0
    For Each datax In range_data
        If datax.Interior.ColorIndex = 1 Then
            CountBlack = CountBlack + 1 ' adds 1 for each that meets criteria
        End If
    Next datax
End Function

Function CountAll(range_data As Range) As Long
    Dim datax As Range
    CountAll = 0
    For Each datax In range_data
        CountAll = CountAll + 1 'adds one for each counted
    Next datax
End Function

(附:问题对应的表格截图)

备注:内容来源于stack exchange,提问作者Jack Johnson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.22 10:59:30