VBA Excel 格式化真的很慢
VBA Excel formatting really really slow
我正在尝试找出一种使用 VBA 格式化单元格的更好方法。我正在尝试替换 Excel Forms Control 中的选项按钮(它工作正常但非常丑陋。它看起来像旧的 W95)。我没有使用 activeX,因为我不能相信如果我使用 activex,每个用户都能打开此工作簿。
所以,我试图使用一些 VBA 来像按下按钮一样格式化单元格。将 SUB 格式化为按下格式并将另一个格式化为未按下应该很容易。但是这段代码是运行不少于13秒!这是不可行的。
我做了一些研究并找到了其他主题,包括“VBA 代码优化”
VBA code optimization
其他主题:
slow cell formatting using vba? 但我的情况只是边框和单元格颜色
Extremely slow VBA code when formatting cells 我从哪里得到优化代码
这是代码
Sub BtnSelect()
Dim t
t = Timer
With Selection
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Color = -1740185
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Color = -1740185
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = -736322
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Color = -736322
.TintAndShade = 0
.Weight = xlMedium
End With
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 16576494
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Debug.Print Timer - t
End Sub
我做错了什么?这个简单的操作需要这么长时间,这有点愚蠢。非常感谢!
这对我来说不到 0.02 秒
Sub BtnSelect()
Dim t
t = Timer
With Selection
With .Borders()
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlMedium
End With
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
.Borders(xlEdgeLeft).Color = -1740185
.Borders(xlEdgeTop).Color = -1740185
.Borders(xlEdgeBottom).Color = -736322
.Borders(xlEdgeRight).Color = -736322
.Interior.Color = 16576494
End With
Debug.Print Timer - t
End Sub
我正在尝试找出一种使用 VBA 格式化单元格的更好方法。我正在尝试替换 Excel Forms Control 中的选项按钮(它工作正常但非常丑陋。它看起来像旧的 W95)。我没有使用 activeX,因为我不能相信如果我使用 activex,每个用户都能打开此工作簿。
所以,我试图使用一些 VBA 来像按下按钮一样格式化单元格。将 SUB 格式化为按下格式并将另一个格式化为未按下应该很容易。但是这段代码是运行不少于13秒!这是不可行的。
我做了一些研究并找到了其他主题,包括“VBA 代码优化”
VBA code optimization
其他主题:
slow cell formatting using vba? 但我的情况只是边框和单元格颜色
Extremely slow VBA code when formatting cells 我从哪里得到优化代码
这是代码
Sub BtnSelect()
Dim t
t = Timer
With Selection
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
With .Borders(xlEdgeLeft)
.LineStyle = xlContinuous
.Color = -1740185
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeTop)
.LineStyle = xlContinuous
.Color = -1740185
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeBottom)
.LineStyle = xlContinuous
.Color = -736322
.TintAndShade = 0
.Weight = xlMedium
End With
With .Borders(xlEdgeRight)
.LineStyle = xlContinuous
.Color = -736322
.TintAndShade = 0
.Weight = xlMedium
End With
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
With .Interior
.Pattern = xlSolid
.PatternColorIndex = xlAutomatic
.Color = 16576494
.TintAndShade = 0
.PatternTintAndShade = 0
End With
End With
Debug.Print Timer - t
End Sub
我做错了什么?这个简单的操作需要这么长时间,这有点愚蠢。非常感谢!
这对我来说不到 0.02 秒
Sub BtnSelect()
Dim t
t = Timer
With Selection
With .Borders()
.LineStyle = xlContinuous
.TintAndShade = 0
.Weight = xlMedium
End With
.Borders(xlDiagonalDown).LineStyle = xlNone
.Borders(xlDiagonalUp).LineStyle = xlNone
.Borders(xlInsideVertical).LineStyle = xlNone
.Borders(xlInsideHorizontal).LineStyle = xlNone
.Borders(xlEdgeLeft).Color = -1740185
.Borders(xlEdgeTop).Color = -1740185
.Borders(xlEdgeBottom).Color = -736322
.Borders(xlEdgeRight).Color = -736322
.Interior.Color = 16576494
End With
Debug.Print Timer - t
End Sub