颜色计数后性能不佳

时间:2018-08-20 07:44:31

标签: excel excel-vba excel-formula

在将此代码实现到我的Excel列表中以计算在设置范围内没有颜色的已填充单元格后,我的性能出现问题:

Function CountCcolor(range_data As Range, criteria As Range) As Long
    Dim datax As Range
    Dim xcolor As Long
    xcolor = criteria.Interior.ColorIndex
    For Each datax In range_data
        If datax.Interior.ColorIndex = xcolor And Not datax.Value = vbNullString Then
           CountCcolor = CountCcolor + 1
        End If
    Next datax
End Function

我也用它来计数设置范围内同一页面上的黄色和红色单元,但是它并没有像上面的那样降低性能:

Function Farbsumme(Bereich As Range, Farbe As Integer)
    Dim Zelle As Range
    Application.Volatile
    For Each Zelle In Bereich
        If Zelle.Interior.ColorIndex = Farbe Then
            Farbsumme = Farbsumme + 1
        End If
    Next
End Function

我做错了什么吗?我有什么可以做得更好以提高性能?

1 个答案:

答案 0 :(得分:4)

您可能正在使用工作表中的另一个函数,并且每次重新计算application.volatile都会降低代码的速度。

删除application.volatile可能会解决您的问题。