【问题标题】:Slow VBA loop for hiding rows [duplicate]用于隐藏行的慢速 VBA 循环[重复]
【发布时间】:2021-08-19 03:27:07
【问题描述】:

以下代码用于循环遍历范围内的所有行并根据该单元格中的单元格值隐藏它们,以及下面的一个。如果两个单元格值 = "" 则意图是隐藏整行。一切正常,但速度非常慢。任何关于更快的建议将不胜感激。

Sheets("Morning Report Export Sheet").Activate

For x = 10 To 108
    If Cells(x, 9).Value = "" Then
        If Cells(x + 1, 9).Value = "" Then
            Cells(x, 9).EntireRow.Hidden = True
        End If
    End If
Next

...也尝试了以下方法,但同样慢...

If Cells(x, 9).Value = "" And Cells(x + 1, 9).Value = "" Then

【问题讨论】:

  • 将第 9 列的范围拉入变量数组(您必须将其转置为一维数组),然后迭代该数组的值,以及当条件遇到你Union 对应的Range 到内存中的单元格范围(注意:循环除了用所有相关单元格构建一个联合范围对象之外什么都不做);在循环之后,您然后转到theUnionRange.EntireRow.Hidden = True 并且噗,您刚刚隐藏了所有需要在单个操作中隐藏的行,从工作表 once 读取,并写入工作表 once .
  • 或者您可以通过在循环之前关闭Application.ScreenUpdating 并在循环之后重新打开它来掩盖问题并继续不断地点击工作表,但我敢打赌,如果你可以使用Union 方法。

标签: excel vba loops hide


【解决方案1】:

我写了两个版本,因为我很无聊。

这是@MathieuGuindon提到的数组方法

Sub HideRowsUsingArrays()
    Dim x As Long, HideRows As Range
    Dim StartRow As Long, EndRow As Long, Col As Long
    
    'TARGET RANGE
    Col = 9
    StartRow = 10
    EndRow = 108
    'TARGET RANGE
    
    Dim sh As Worksheet
    Set sh = Sheets("Morning Report Export Sheet")
    
    Dim vArr() As Variant
    'Saving all values in the target range to an array
    vArr = sh.Cells(StartRow, Col).Resize(EndRow).Value
    
    'Looping through the array
    For x = LBound(vArr) To UBound(vArr) - 1
        'If val or next val is empty
        If vArr(x) = "" And vArr(x + 1) = "" Then
            'Add the corresponding row to HideRows range
            'Union causes an error if HideRows is nothing, so the first iteration cant use Union
            If HideRows Is Nothing Then
                Set HideRows = sh.Rows(x + StartRow - 1).EntireRow
            Else
                Set HideRows = Union(HideRows, sh.Rows(x + StartRow - 1).EntireRow)
            End If
        End If
    Next x
    
    'Hide the gathered rows
    If Not HideRows Is Nothing Then HideRows.EntireRow.Hidden = True
End Sub

这是@TimWilliams 链接到的 Range 方法

Sub HideRowsUsingRanges()
    Dim cell As Range, HideRows As Range
    Dim StartRow As Long, EndRow As Long, Col As Long
    
    'TARGET RANGE
    Col = 9
    StartRow = 10
    EndRow = 108
    'TARGET RANGE
    
    Dim sh As Worksheet
    Set sh = Sheets("Morning Report Export Sheet")
    
    Dim r As Range
    'Saving the target range
    set r = sh.Cells(StartRow, Col).Resize(EndRow)
    
    'Looping through each cell of the range
    For Each cell In r
        'If val or next val is empty
        If cell.Value = "" And cell.Offset(1).Value = "" Then
            'Add the corresponding row to HideRows range
            'Union causes an error if HideRows is nothing, so the first iteration cant use Union
            If HideRows Is Nothing Then
                Set HideRows = cell
            Else
                Set HideRows = Union(HideRows, cell)
            End If
        End If
    Next cell
    
    'Hide the gathered rows
    If Not HideRows Is Nothing Then HideRows.EntireRow.Hidden = True
End Sub

【讨论】:

  • 底部选项工作得更快,谢谢。 Top 1 运行失败,超出 Range 上线:If vArr(x).Value = "" And vArr(x + 1).Value = "" Then
  • 哦,哎呀,那些.Values 不应该在那里。我会删除它们
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-11
  • 2015-03-28
  • 2019-05-15
相关资源
最近更新 更多