首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ColorFunction UDF

ColorFunction UDF
EN

Stack Overflow用户
提问于 2015-10-12 09:24:01
回答 1查看 247关注 0票数 0

嗨,我尝试了3种不同类型的颜色函数UDF,它们可以在我的Excel 2013网上找到。但是每次我刷新时它都会崩溃.有一个修复程序来停止此操作(只有在手动完成时,excel才能刷新它)

这是代码:

代码语言:javascript
复制
    Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
    Dim rCell As Range
    Dim lCol As Long
    Dim vResult

    lCol = rColor.Interior.ColorIndex
    If SUM = True Then
        For Each rCell In rRange
            If rCell.Interior.ColorIndex = lCol Then
                vResult = WorksheetFunction.SUM(rCell,vResult)
            End If
        Next rCell
    Else
        For Each rCell In rRange
            If rCell.Interior.ColorIndex = lCol Then
                vResult = 1 + vResult
            End If
        Next rCell
    End If
   ColorFunction = vResult
End Function

请帮忙,因为这件事很烦人,我的电脑都崩溃了.

能否将其放入我可以手动运行的宏中?这能解决问题吗?

额外信息-运行windows 8.1.2013年办公室。我已经尝试过在三台不同的计算机上运行,同样的情况也发生在windows 7上的2010版office上。仅仅是崩溃excel试图更新(可能太多的记录,但它们包含大约100行,应该可以吗?)

尝试了以下操作,其中也崩溃了excel。只是说计算(3 PRCOESSOR(S));0%

代码语言:javascript
复制
    Function CountCellsByColor(rData As Range, cellRefColor As Range) As Long
    Dim indRefColor As Long
    Dim cellCurrent As Range
    Dim cntRes As Long

    Application.Volatile
    cntRes = 0
    indRefColor = cellRefColor.Cells(1, 1).Interior.Color
    For Each cellCurrent In rData
        If indRefColor = cellCurrent.Interior.Color Then
            cntRes = cntRes + 1
        End If
    Next cellCurrent

    CountCellsByColor = cntRes
End Function

它最终起作用了,但每个人至少需要3分钟的时间.因此,当它试图更新40个带有颜色函数的字段时,整个事件就会崩溃。

在任务管理器中查看并遵循等待链,如果这是问题的话,就会出现plwow64.exe的一些想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-10-12 10:34:46

我会说,这很有可能是另一个事件被触发,并正在进入一个没完没了或非常广泛的循环。

通过禁用应用程序事件来测试它,看看您的函数是否运行得更快。我整理了一下您的代码,并给出了一个示例,说明如何为您的测试禁用事件。当然,当您完成任务时,请记住启用这些事件。

代码语言:javascript
复制
Public Function ColorFunction(rColor As Range, rRange As Range, Optional isAggregating As Boolean) As Variant
    Dim rCell As Range
    Dim iRefColourIndex As Integer
    Dim result As Variant

    'Try toggling this line false and true.
    'If there's a big speed difference then you must have a _Change event causing you trouble.
    Application.EnableEvents = False

    iRefColourIndex = rColor.Interior.ColorIndex
    result = 0
    For Each rCell In rRange.Cells
        If rCell.Interior.ColorIndex = iRefColourIndex Then
            If isAggregating And IsNumeric(rCell.Value2) Then
                result = result + rCell.Value2
            Else
                result = result + 1
            End If
        End If
    Next

    ColorFunction = result

End Function
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33077550

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档