【发布时间】:2019-10-09 12:44:04
【问题描述】:
我有一个 Worksheet_Change 宏,它根据用户在具有数据验证列表的单元格中所做的选择来隐藏/取消隐藏行。
代码需要一分钟才能运行。它循环超过 c.2000 行。我希望它需要几秒钟的时间,以便它成为一个有用的用户工具。
Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
'Exit the routine early if there is an error
On Error GoTo EExit
'Manage Events
Application.ScreenUpdating = False
Application.DisplayAlerts = False
Application.EnableEvents = False
'Declare Variables
Dim rng_DropDown As Range
Dim rng_HideFormula As Range
Dim rng_Item As Range
'The reference the row hide macro will look for to know to hide the row
Const str_HideRef As String = "Hide"
'Define Variables
'The range that contains the week selector drop down
Set rng_DropDown = Range("rng_WeekSelector")
'The column that contains the formula which indicates if a row should
'be hidden c.2000 rows
Set rng_HideFormula = Range("rng_HideFormula")
'Working Code
'Exit sub early if the Month Selector was not changed
If Not Target.Address = rng_DropDown.Address Then GoTo EExit
'Otherwise unprotect the worksheet
wks_DailyPlanning.Unprotect (str_Password)
'For each cell in the hide formula column
For Each rng_Item In rng_HideFormula
With rng_Item
'If the cell says "hide"
If .Value2 = str_HideRef Then
'Hide the row
.EntireRow.Hidden = True
Else
'Otherwise show the row
.EntireRow.Hidden = False
End If
End With
'Cycle through each cell
Next rng_Item
EExit:
'Reprotect the sheet if the sheet is unprotected
If wks_DailyPlanning.ProtectContents = False Then wks_DailyPlanning.Protect (str_Password)
'Clear Events
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Application.EnableEvents = True
End Sub
我查看了该网站上其他用户提供的一些链接,我认为问题在于我必须单独遍历每一行。
是否可以创建类似 .visible 设置的数组,我可以一次将其应用于整个范围?
【问题讨论】:
-
您可以从硬盘与内存的Understanding Read/ Write Speeds 中受益。
-
如果您在该行的(隐藏)列中有可见计算的结果,则一次性将
.autofilter应用于所有错误值。这比遍历所有这些要快得多。 -
我喜欢
AutoFilter的想法,或者循环遍历一个数组并使用Union创建两个范围对象来一次隐藏/取消隐藏。这比遍历项目并一一隐藏/取消隐藏行要快。此外,工作代码可能更适合Code Review =) -
此外,如果您需要隐藏行而不是过滤行(有一点不同),那么可以使用
.advancedfilter来完成。 Shameless self plug
标签: excel vba optimization foreach