2016-10-31 116 views
0

所以我有下面的VBA宏設置,當CompHide運行時需要幾分鐘時間來更新。我覺得這是由於C.EntireRow.Columns(43).Value =「」加速VBA執行

這一行我試圖做一個新的「幫手」列,檢查是否兩列都是空的,並有它返回「Y」或「N」,然後讓宏查看「Y」並隱藏它們。這加快了速度,但我希望如果可以的話速度更快。

原單碼:

Sub CompHide() 

    Dim sht As Worksheet, C As Range 

    Application.ScreenUpdating = False 
    Application.EnableEvents = False 

    Set sht = Sheets("Comparison") 
     sht.Rows.Hidden = False 

    CSetRowVis "C9", "CMarket1" 
    CSetRowVis "C115", "CMarket2" 
    CSetRowVis "C221", "CMarket3" 
    CSetRowVis "C329", "CMarket4" 
    CSetRowVis "C437", "CMarket5" 
    CSetRowVis "C545", "CMarket6" 
    CSetRowVis "C653", "CMarket7" 
    CSetRowVis "C761", "CMarket8" 
    CSetRowVis "C869", "CMarket9" 
    CSetRowVis "C977", "CMarket10" 

    For Each C In sht.Range("CNonTest") 
     If C.Value = "" And C.EntireRow.Columns(43).Value = "" Then 
      C.EntireRow.Hidden = True 
     End If 
    Next 

    sht.Range("CBlank").EntireRow.Hidden = True 

    Application.EnableEvents = True 
    Application.ScreenUpdating = True 

End Sub 

Sub CSetRowVis(addr As String, rngName As String) 
    With Sheets("Comparison") 
     If .Range(addr).Value = "Unused" Then 
      .Range(rngName).EntireRow.Hidden = True 
     End If 
    End With 
End Sub 

新代碼:

Sub CompHide() 

    Dim sht As Worksheet, C As Range 

    Application.ScreenUpdating = False 
    Application.EnableEvents = False 

    Set sht = Sheets("Comparison") 
     sht.Rows.Hidden = False 

    CSetRowVis "C9", "CMarket1" 
    CSetRowVis "C115", "CMarket2" 
    CSetRowVis "C221", "CMarket3" 
    CSetRowVis "C329", "CMarket4" 
    CSetRowVis "C437", "CMarket5" 
    CSetRowVis "C545", "CMarket6" 
    CSetRowVis "C653", "CMarket7" 
    CSetRowVis "C761", "CMarket8" 
    CSetRowVis "C869", "CMarket9" 
    CSetRowVis "C977", "CMarket10" 

    For Each C In sht.Range("CHideTest") 
     If C.Value = "Y" Then 
      C.EntireRow.Hidden = True 
     End If 
    Next 

    sht.Range("CBlank").EntireRow.Hidden = True 

    Application.EnableEvents = True 
    Application.ScreenUpdating = True 

End Sub 

Sub CSetRowVis(addr As String, rngName As String) 
    With Sheets("Comparison") 
     If .Range(addr).Value = "Unused" Then 
      .Range(rngName).EntireRow.Hidden = True 
     End If 
    End With 
End Sub 

這是使用Excel 2013標準64位。行數剛剛低於1200.列數爲150.所有這些單元都是公式

如果有任何額外的信息,您需要讓我知道。

+0

我投票作爲題外話,因爲它屬於在代碼審查 –

+0

代碼評審關閉這個問題是一個事?我不知道這個。你能指出我的網址先生 –

+0

http://codereview.stackexchange.com/ :) –

回答

2

取而代之的是:

For Each C In sht.Range("CHideTest") 
    If C.Value = "Y" Then 
     C.EntireRow.Hidden = True 
    End If 
Next 

考慮這樣的事情:

Dim rng As Range 'for collecting rows to be hidden 

For Each C In sht.Range("CHideTest") 
    If C.Value = "Y" Then 
     if rng Is Nothing Then 
      set rng = C 
     Else 
      set rng = application.union(rng, C) 
     end if 
    End If 
Next 

'hide all accumulated rows (if any found) 
if not rng is nothing then rng.EntireRow.Hidden = True 
+0

另外,將'If C.Value =「Y」位轉換爲變量數組 –

+0

我會測試這個本週晚些時候。工作已經分配了一個更大的項目,我現在必須努力加以解決。 –

+0

能夠測試並將執行時間降至30秒。我看到一些其他的代碼,我可能會修剪,以加快速度更感謝幫助。 –