2016-01-23 93 views
2

我有一張表,我想完全隱藏或隱藏/顯示錶中的行,具體取決於單元格值是否爲0或更高。基於值的緩慢宏隱藏行

它在單元格D26中查找值0;如果0它隱藏行24-51,如果不爲0,它隱藏/顯示根據是否有位於行34和49

下面的宏的位置在c列中的值是成爲一個可行的選擇太慢行。任何人都可以提出一種替代方法來做到這一點,這可能會在幾秒鐘內工作,而不是幾分鐘?我認爲這是因爲我正在運行For/If/Else循環。

Sub HideManifolds() 
' 
' HideManifolds Macro 
' 
Application.ScreenUpdating = False 
Application.EnableEvents = False 
Application.Calculation = xlCalculationManual 

ChkCol = 3 
Manifold1BeginTableRow = 34 
Manifold1EndTableRow = 49 
Manifold1BeginRow = 24 
Manifold1EndRow = 51 

    For Manifold1RowCnt = Manifold1BeginRow To Manifold1EndRow 
     If Cells(26, 4).Value = 0 Then 
      Cells(Manifold1RowCnt, 1).EntireRow.Hidden = True 
     Else 
      For Manifold1TableRowCnt = Manifold1BeginTableRow To Manifold1EndTableRow 
       If Cells(Manifold1TableRowCnt, ChkCol).Value = 0 Then 
        Cells(Manifold1TableRowCnt, ChkCol).EntireRow.Hidden = True 
       Else 
        Cells(Manifold1TableRowCnt, ChkCol).EntireRow.Hidden = False 
       End If 
      Next Manifold1TableRowCnt 
     End If 
    Next Manifold1RowCnt 

Application.ScreenUpdating = True 
Application.EnableEvents = True 
Application.Calculation = xlCalculationAutomatic 
' 
End Sub 
+1

請參閱[設置自動過濾多個通配符(http://stackoverflow.com/questions/16602872/set-auto-filtering-multiple-wildcards),用於設置[AutoFilter](https://msdn.microsoft.com/en-us/library/office/aa221844.aspx)條件的方法,使用字典的鍵(或項目)。 – Jeeped

+0

我不知道這個小數據代碼如何花費幾分鐘,是否有很多條件格式? – Fadi

+0

@Fadi忘了說,它在技術上做了6次這個宏,我在同一張表上有6張表。我只是把它發佈爲一個,因爲它們都是相互重複的(除了行/單元格數量變化以外)。 – iAGPx

回答

1

我覺得你不需要這個循環對於Manifold1RowCnt = Manifold1BeginRow To Manifold1EndRow

代碼:

Sub HideManifolds() 
' 
' HideManifolds Macro 
' 
Dim hRng As Range, vRng As Range 

Application.ScreenUpdating = False 
Application.EnableEvents = False 
Application.Calculation = xlCalculationManual 

ChkCol = 3 
Manifold1BeginTableRow = 34 
Manifold1EndTableRow = 49 
Manifold1BeginRow = 24 
Manifold1EndRow = 51 

     If Cells(26, 4).Value = 0 Then 
      Rows(Manifold1BeginRow & ":" & Manifold1EndRow).Hidden = True 
     Else 
      For Manifold1TableRowCnt = Manifold1BeginTableRow To Manifold1EndTableRow 
       If Cells(Manifold1TableRowCnt, ChkCol).Value = 0 Then 

        If hRng Is Nothing Then 
        Set hRng = Cells(Manifold1TableRowCnt, ChkCol) 
        Else 
        Set hRng = Union(hRng, Cells(Manifold1TableRowCnt, ChkCol)) 
        End If 

       Else 

        If vRng Is Nothing Then 
        Set vRng = Cells(Manifold1TableRowCnt, ChkCol) 
        Else 
        Set vRng = Union(vRng, Cells(Manifold1TableRowCnt, ChkCol)) 
        End If 

       End If 
      Next Manifold1TableRowCnt 

      If Not hRng Is Nothing Then hRng.EntireRow.Hidden = True 
      If Not vRng Is Nothing Then vRng.EntireRow.Hidden = False 

     End If 



Application.ScreenUpdating = True 
Application.EnableEvents = True 
Application.Calculation = xlCalculationAutomatic 
' 
End Sub