简体   繁体   English

根据单元格值隐藏行

[英]Hide rows based on cell value

I have a table of items and quantities, where I want to hide rows when the quantity is 0. The macro works, but it takes too long to complete. 我有一个项目和数量表,我想在数量为0时隐藏行。宏工作,但完成时间太长。

This is the code: 这是代码:

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

Is there a more efficient way of getting the same result, of hiding rows 414-475 if the value in column X is 0? 如果X列中的值为0,是否有更有效的方法来获得相同的结果,隐藏行414-475?

The common way to make any code (that does any changing to the workbook) faster is by disabling screen updating and disabling events and changing the calculation mode to Manual (there are other ways, but these 3 things have the biggest factor). 制作任何代码(对工作簿进行任何更改)的常用方法是禁用screen updating和禁用events ,并将calculation模式更改为Manual (还有其他方法,但这三个因素最重要)。

And the the other thing is by collecting all rows in one union range has a big factor in deleting and inserting rows because the time that is needed to delete one row is similar to the time for deleting the whole union range. 而另一件事是通过收集一个联合范围中的所有行在删除和插入行中有一个重要因素,因为删除一行所需的时间与删除整个联合范围的时间相似。 For example if deleting one row needs 1 second then deleting 1000 rows will need 1000 seconds, but deleting a union range that contains 1000 rows only needs 1 second. 例如,如果删除一行需要1秒,则删除1000行将需要1000秒,但删除包含1000行的联合范围只需要1秒。

Try this code: 试试这段代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM