颜色计数后性能不佳

Psy*_*sun 7 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
Run Code Online (Sandbox Code Playgroud)

我也使用这个来计算设定范围内同一页面上的黄色和红色单元格,但它不会像上面那样降低性能:

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
Run Code Online (Sandbox Code Playgroud)

有什么我做错了吗?我能做些什么来提高性能?

小智 4

您可能正在使用工作表中的其他函数,并且每次它重新计算都会application.volatile减慢您的代码速度。

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

  • [此处](https://learn.microsoft.com/en-us/office/vba/api/Excel.Application.Volatile)是有关此函数的一些很好的简单信息,以供进一步解释:) (2认同)