首页 文章

VBA单元格条件与手动格式 - 最佳实践

提问于
浏览
0

我在VBA中有一个函数来根据指定的值格式化单元格内部颜色 - 负数和正数以及零 .

PositiveFillColor,NeutralFillColor和NegativeFillColor是从读取颜色放入设置表单元格的全局长变量 .

我主要担心的是宏的速度(对于中等数量的数据显然看起来非常好)和工作簿大小(对于这个数据量,3,5 MB似乎太多) .

也许在VBA中使用Excel条件格式是一种更好的做法?

Public Function FillColorByValue(ByVal RefNumber As Double) As Long

Dim FillColor As Long

    If RefCellValue > 0 Then
        FillColor = PositiveFillColor
    ElseIf RefCellValue = 0 Then
        FillColor = NeutralFillColor
    ElseIf RefCellValue < 0 Then
        FillColor = NegativeFillColor
    End If

FillColorByValue = FillColor

End Function

1 回答

  • 0

    尝试两种方式,看看哪一种更快

    sub thetimingstuff()
    
    Dim StartTime As Double
    Dim SecondsElapsed As Double
    
     StartTime = Timer
    
    'your code goes here
    
    SecondsElapsed = Round(Timer - StartTime, 2)
    MsgBox "This code ran successfully in " & SecondsElapsed & " seconds", vbInformation
    
    end sub
    

相关问题