根据单元格值隐藏行

我有一个项目和数量的表格,当数量为0时,我想要隐藏行。macros观工作,但需要很长时间才能完成。

这是代码:

Sub Hide2ndFix() ' ' Hide2ndFix Macro ' BeginRow = 414 EndRow = 475 ChkCol = 24 For RowCnt = BeginRow To EndRow If Cells(RowCnt, ChkCol).Value = 0 Then Cells(RowCnt, ChkCol).EntireRow.Hidden = True End If Next RowCnt ' End Sub 

是否有一个更有效的方式获得相同的结果,隐藏行414-475如果列中的值是0?

通过禁用screen updating和禁用events并将calculation模式更改为Manual (还有其他方法,但这三件事情具有最大的影响因素),使代码(对工作簿进行任何更改)的常用方法是更快。

而另一件事是通过收集一个联合范围中的所有行在删除和插入行方面有一个很大的因素,因为删除一行所需的时间与删除整个联合范围的时间类似。 例如,如果删除一行需要1秒,那么删除1000行需要1000秒,但删除包含1000行的联合范围只需要1秒。

试试这个代码:

 Sub Hide2ndFix() ' ' Hide2ndFix Macro ' Dim RowCnt As Long, uRng As Range Application.ScreenUpdating = False Application.EnableEvents = False Application.Calculation = xlCalculationManual BeginRow = 414 EndRow = 475 ChkCol = 24 For RowCnt = BeginRow To EndRow If Cells(RowCnt, ChkCol).Value = 0 Then If uRng Is Nothing Then Set uRng = Cells(RowCnt, ChkCol) Else Set uRng = Union(uRng, Cells(RowCnt, ChkCol)) End If End If Next RowCnt ' If Not uRng Is Nothing Then uRng.EntireRow.Hidden = True Application.ScreenUpdating = True Application.EnableEvents = True Application.Calculation = xlCalculationAutomatic End Sub