【问题标题】:VBA Excel formatting really really slowVBA Excel格式化真的很慢
【发布时间】:2021-08-19 17:54:59
【问题描述】:

我正在尝试找出一种使用 VBA 格式化单元格的更好方法。我正在尝试从 Excel 表单控件中替换一个选项按钮(它工作正常但非常难看。它看起来像旧的 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

我做错了什么?这个简单的操作需要这么长时间,有点愚蠢。非常感谢!

【问题讨论】:

  • 不看代码我们怎么可能帮忙?
  • 对不起!我的坏...
  • 我刚刚在整张单元格上对其进行了测试,并且成功了。 1.8 秒。不知道为什么你会得到 13 秒。
  • 您可以只使用With .Borders(),它将覆盖左/右/上/下边框
  • ...哎呀,我看到您对每个边框使用了不同的设置,所以这行不通...

标签: excel vba format cell


【解决方案1】:

这对我来说运行时间

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

【讨论】:

    猜你喜欢
    • 2016-10-17
    • 2016-11-22
    • 2014-07-10
    • 1970-01-01
    • 2019-06-21
    • 1970-01-01
    • 2017-11-10
    • 2010-12-28
    • 2013-01-25
    相关资源
    最近更新 更多