【发布时间】:2016-03-29 04:35:24
【问题描述】:
我的第一个问题:)
有一个包含 3000 行的工作表,每次激活工作表时都需要检查和隐藏。
通常只有 100 行是可见的,但我必须确保它始终是足够的行。 (以防万一)。
我的代码运行良好,但速度有点慢。加快速度的提示会很棒。
Private Sub Worksheet_Activate()
On Error GoTo ExitHandling
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
'Hide Operations columns if no values
If Worksheets("BasicData").Range("CheckOperationsZero").Value = "Yes" Then
Columns("I:J").EntireColumn.Hidden = True
Else
Columns("I:J").EntireColumn.Hidden = False
End If
'Hide empty rows, dont hide if row belowe is not empty, autofit for better viewing
ActiveSheet.Rows("17:3017").EntireRow.Hidden = False
For I = 3016 To 18 Step -1
If Application.WorksheetFunction.CountIf(Range("B" & I & ":J" & I), vbNullString) >= 9 And Application.WorksheetFunction.CountIf(Range("B" & I + 1 & ":J" & I + 1), vbNullString) >= 9 Then
Rows(I).RowHeight = 12
Rows(I).EntireRow.Hidden = True
Else
Rows(I).EntireRow.AutoFit
If Rows(I).Height < 20 Then
Rows(I).RowHeight = 12
End If
End If
Next I
ExitHandling:
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
Exit Sub
ErrorHandling:
MsgBox Err.Description
Resume ExitHandling
End Sub
【问题讨论】:
-
为什么不直接找到行数然后隐藏呢?
-
为什么要循环?一次全部完成。
ActiveSheet.Rows("17:3017").EntireRow.Hidden = True和Rows("17:3017").RowHeight = 12
标签: performance vba excel for-loop