更快地隐藏空行
我试图隐藏A列中单元格的值为空(即空)的所有行。 我正在尝试使用下面的代码:
Range("A7:A117").SpecialCells(xlCellTypeBlanks).EntireRow.Hidden = True
但是,列A中的每个单元格都有一个VLOOKUP
公式,而xlCellTypeBlanks
将一个单元格视为具有公式的单元格,但没有值, 不能为空。
所以我尝试使用下面的代码,但它非常缓慢。
For i = 17 To 117 If ActiveSheet.Cells(i, 1) = "" Then ActiveSheet.Cells(i, 1).EntireRow.Hidden = True End If
我如何加快速度?
你为什么不尝试AutoFilter:
Range("A7:A117").AutoFilter 1, "<>", , , False
这不是for循环,而是慢慢的,每次更改屏幕时都会更新屏幕(这使用了一定的处理能力,从而减慢了一切)。 如果在隐藏行之前closures屏幕更新,那么只要更新一次,脚本运行速度就会快得多。 我试了100行,几乎是瞬间的。
Sub hideEmptyRows() Application.ScreenUpdating = False For i = 1 To 117 If ActiveSheet.Cells(i, 1) = "" Then ActiveSheet.Cells(i, 1).EntireRow.Hidden = True End If Next i Application.ScreenUpdating = True End Sub
Range("A7:A117").AutoFilter 1, "<>", , , False
它隐藏空单元格,但如果你试图用鼠标取消隐藏你不能
这里没有自动filter的答案:
Dim totalRange As Range ActiveSheet.Range("A17:A117").Hidde = false For Each cell In ActiveSheet.Range("A17:A117") If cell = "" And totalRange Is Nothing Then Set totalRange = cell ElseIf cell = "" Then Set totalRange = Application.union(totalRange, cell) End If Next If Not totalRange Is Nothing Then totalRange.EntireRow.Hidden = True End If