簡體   English   中英

基於值的宏隱藏行慢

[英]Slow macro hiding rows based on value

我有一個表,要根據單元格值是0還是大於0,完全隱藏或隱藏/顯示表中的行。

它在單元格D26中查找值為0;否則為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

我認為您不需要此循環For 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM