VBA中的Font.Color正在降低Excel的速度

时间:2014-04-03 18:15:30

标签: vba excel-2010

我在VBA for Excel中编写了一个函数,根据2个不同的值为单元格的字体分配字母和颜色代码。

基本上,该函数有2个参数用于比较值和包含字母和颜色的单元格。我使用webdings字体,所以字母看起来像一个符号。

我知道这可以通过conditionnal格式完成,但是这个函数将在我的工作簿中大量调用,在多个工作表中,我发现随着时间的推移使用VBA函数更容易维护并将所有内容放在同一个的地方。

问题:“currentCell.Font.Color”的使用正在缓慢地降低Excel的速度。即使我在工作簿打开时初始化了常量颜色,也使用了我在网上找到的一些优化技巧等。

你知道为什么字体颜色的变化会大大减慢Excel的速度吗?

此致

Const DefUnderBudget = 0.95
Const DefOverBudget = 1.05
Const FavMoyen = 0.98
Const DefMoyen = 1.02
Const FavOverBudget = 1.05
Const FavUnderBudget = 0.95


Const SMALL = "="
Const BIG = "n"


Global RED As Long
Global BLUE As Long
Global YELLOW As Long




Sub InitColors()
    RED = RGB(218, 150, 148)
    BLUE = RGB(149, 179, 215)
    YELLOW = RGB(243, 202, 38)
End Sub



Function setPerformanceIndicator(actualVal As Double, compVal As Double, currentCell As Range) As String


Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = False



        If actualVal < compVal * DefUnderBudget Then

           currentCell.Font.Color = RED
           setPerformanceIndicator = BIG

        ElseIf actualVal < compVal * FavMoyen Then

           currentCell.Font.Color = YELLOW
           setPerformanceIndicator = SMALL

        ElseIf actualVal > compVal * FavOverBudget Then

           currentCell.Font.Color = BLUE
           setPerformanceIndicator = BIG

        Else
           setPerformanceIndicator = vbNullString
        End If



Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True




End Function

1 个答案:

答案 0 :(得分:1)

从技术上讲,Windows可能需要多次刷新屏幕。

在多次通话之前,请运行

Application.ScreenUpdating = False

之后,运行

 Application.ScreenUpdating = True
相关问题