2015-04-02 53 views
1

我有一个应用程序,它运行我需要加速的一段VBA代码。在VBA中优化交替格式化

我环顾四周寻找解决方案,但它非常具体,我找不到任何东西。我将4-3000行的格式更改为可见性。这需要很长时间才能在旧计算机上运行,​​这是该进程通常运行的地方。你们对如何优化它有什么想法吗?

For i = Range(startCell).Row To Range(endCell).Row Step 2 

    Rows(i).Interior.color = RGB(50, 50, 50) 'Grey background 
    Rows(i).Font.color = RGB(0, 240, 255) 'Cyan text 
    Rows(i + 1).Interior.color = RGB(0, 0, 0) 'Black background 
    Rows(i + 1).Font.color = RGB(240, 255, 0) 'Yellow text 

Next 
+0

退房[codereview.se]下一次你有工作代码。 – 2015-04-02 17:55:18

+0

在哪里以及如何分配'startCell'和'endCell'? – 2015-04-02 17:58:59

回答

0

尝试增加这样一个小程序中的代码模块:

Public Sub ToggleWaitMode(Optional ByVal wait As Boolean = True) 
    With Excel.Application 

     .Calculation = IIf(wait, xlCalculationManual, xlCalculationAutomatic) 
     .Cursor = IIf(wait, xlWait, xlDefault) 
     .StatusBar = IIf(wait, "Please wait...", False) 

     .DisplayAlerts = Not wait 
     .ScreenUpdating = Not wait 

    End With 

End Sub 

然后修改代码来调用它:

ToggleWaitMode 
'<your code> 
ToggleWaitMode False 

应该速度了一点东西...或者至少在循环运行时给用户一些反馈。


其它的(也许更好)无代码的替代方法是使用条件格式(使用公式看起来像=(MOD(ROW(),2)=0)。你想优化

+1

谢谢!对不起,我没有登录到我的帐户来评论它,但是这非常奏效。 – Dexterious22 2016-03-09 19:34:49