如何使此代码更高效,以便更快地运行?

问题描述 投票:0回答:1

我试图隐藏行,以便只显示某些零售商数据,由于报告的布局,数据不可过滤。我首先将所有行取消隐藏作为重置,然后手动隐藏与零售商无关的行,直到只有点击的零售商信息为止。

然而,这是一种缓慢的方式,我需要一种更快的方式来理解。没有过滤数据的标准。只需点击按钮上的零售商名称即可。

我的代码显示了手动缓慢的方式。

Sub SummaryRetailer1Only()

Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual

'Resets hidden rows by showing everything.
ActiveSheet.Rows("2:480").EntireRow.Hidden = False

'Hides all rows that don't show data for Retailer1.
ActiveSheet.Rows("18:21").EntireRow.Hidden = True
ActiveSheet.Rows("37:48").EntireRow.Hidden = True
ActiveSheet.Rows("54:57").EntireRow.Hidden = True
ActiveSheet.Rows("73:84").EntireRow.Hidden = True
ActiveSheet.Rows("88:129").EntireRow.Hidden = True
ActiveSheet.Rows("261:376").EntireRow.Hidden = True
ActiveSheet.Rows("390:393").EntireRow.Hidden = True
ActiveSheet.Rows("409:420").EntireRow.Hidden = True
ActiveSheet.Rows("424:427").EntireRow.Hidden = True
ActiveSheet.Rows("443:454").EntireRow.Hidden = True

Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True

End Sub

代码工作正常我只想要一种方式,我假设使用一些变量,以便它运行得更快。

excel vba rows show-hide
1个回答
1
投票

其他方式:

Option Explicit

Sub SummaryRetailer1Only()

    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual

    With ThisWorkbook.Worksheets("Sheet1") '<- It s better to create a with statement with the sheet you want to use insead of activesheet

        'Resets hidden rows by showing everything.
        .Rows("2:480").EntireRow.Hidden = False

        'Hides all rows that don't show data for Retailer1.
        Union(.Rows("18:21"), .Rows("37:48"), .Rows("54:57"), .Rows("73:84"), .Rows("88:129"), .Rows("261:376"), _
                .Rows("390:393"), .Rows("409:420"), .Rows("424:427"), .Rows("443:454")).EntireRow.Hidden = True

    End With

    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.