我有一个项目和数量表,当数量为0时,我想在其中隐藏行。宏有效,但是完成所需的时间太长。
这是代码:
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
如果X列的值为0,是否有一种更有效的方法来获得相同的结果,即隐藏行414-475?
使任何代码(对工作簿进行任何更改)更快的常见方法是禁用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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句