我有一个算法可以正常隐藏所有行,在指定的命名范围内,给定的行的值为0.这很简单:
Public Sub MasquerLignesAZeroRapport(rap As Worksheet)
Dim cell As Range
rap.Rows.Hidden = False
For Each cell In rap.Range("Ra_LignesAZero")
If Round(cell.Value, 0) = 0 Then
cell.EntireRow.Hidden = True
End If
Next cell
End Sub
然而,即使关闭计算和屏幕更新并且我尝试了不同的其他方法但没有成功(使用过滤器并隐藏所有可见行但删除过滤器取消隐藏行,这也需要一些时间)用于将行高设置为0)。
有更快的选择吗?我可以使用这种缓慢的算法,但这将是一个值得欢迎的改进,因为这个宏可以在一次运行中针对1-6个报告运行。
答案 0 :(得分:0)
以下是一些优化:
Public Sub MasquerLignesAZeroRapport(rap As Worksheet)
'Optimization #1: as you pointed out, turning off calculations
' and screen updating makes a difference.
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
rap.Rows.Hidden = False
'Optimization #2: instead of loading each cell as a range,
' with all the associated properties, load JUST the values
' into a 2 dimensional array.
Dim values() As Variant
values = rap.Range("Ra_LignesAZero")
For r = 1 To UBound(values, 1)
For c = 1 To UBound(values, 2)
If Round(values(r,c), 0) = 0 Then
rap.Rows(r).Hidden = True
'Optimization #3: if we have already determined
' that the row should be hidden, no need to keep
' looking at cells in the row - might as well break out of the For:
Exit For
End If
Next
Next
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub