我有一个算法可以很好地隐藏所有行,在指定的命名范围内,给定行的值为 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 个报告运行。
最佳答案
以下是一些优化:
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
关于vba - 根据单元格值隐藏多行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27018324/