有没有办法让这段代码运行得更快?我试图隐藏多个工作表中的空白行。
Option Explicit
Private Sub HideRows_Click()
Dim ws As Worksheet, c As Range
Application.ScreenUpdating = False
On Error Resume Next
For Each ws In ThisWorkbook.Worksheets
Select Case ws.Name
Case "Sheet1", "Sheet2", "Sheet3"
'sheets to exclude
'do nothing
Case Else 'hide rows on these sheets
For Each c In ws.Range("AJ16:AJ153,AJ157:AJ292")
c.EntireRow.Hidden = c.Value = 0
Next c
End Select
Next ws
Application.ScreenUpdating = True
End Sub
以下是为了加快速度而对代码所做的一些更改:
- 关闭计算、事件和状态栏
- 首先将所有值分组
AJ
没有值通过Union()
功能和then呼叫EntireRow.Hide
在该组合范围内
老实说,一开始代码就非常干净!
Option Explicit
Private Sub HideRows_Click()
With Application
.Calculation = xlCalculationManual
.ScreenUpdating = False
.DisplayStatusBar = False
.EnableEvents = False
End With
'On Error Resume Next
Dim ws As Worksheet
For Each ws In Worksheets
Select Case ws.name
Case "Sheet1", "Sheet2", "Sheet3" 'sheets to exclude
'do nothing
Case Else 'hide rows on these sheets
Dim unioned As Range
Set unioned = Nothing
Dim c As Range
For Each c In ws.Range("AJ16:AJ153,AJ157:AJ292")
If Len(c.Value2) = 0 Then
If unioned Is Nothing Then
Set unioned = c
Else
Set unioned = Union(unioned, c)
End If
End If
Next c
unioned.EntireRow.Hidden = True
End Select
Next ws
With Application
.Calculation = xlCalculationAutomatic
.ScreenUpdating = True
.DisplayStatusBar = True
.EnableEvents = True
End With
End Sub
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)