根据单元格值隐藏多行

我有一个algorithm,可以很好地隐藏所有的行,在指定的命名范围内,给定的行的值为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 

但是,即使在计算和屏幕更新closures的情况下,这也需要一些时间,并且我已经尝试了不同的其他方法而没有成功(使用filter并隐藏所有可见的行,但是删除filter来取消隐藏行,同样也可以设置行高为0)。

有更快的select吗? 我可以接受这个慢速的algorithm,但这是一个值得欢迎的改进,因为这个macros可以一次运行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