excel - 我怎样才能加快循环

标签 excel vba performance loops

它运行得很好,但执行速度比我想要的慢,我不知道为什么。我还有另一段代码也可以扭转这种情况......

它循环的只有 5 张纸,而且只有在给定的范围内才能启动。它仅检查 A 列中的空值,如果为空,则隐藏该行。

我已经尝试关闭计算、事件和屏幕更新,但它仍然不快......我是否错过了某个地方的内存消耗?它的行为就像它想要崩溃,但随后继续......

    Sub HideBlanks()
    Dim Sheet As Worksheet
    Dim r As Long

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    For Each Sheet In Worksheets
        If Sheet.Index > 1 Then
            With Sheet
                For r = 4 To 350
                    With activesheets
                        If Range("A" & r) = "" Then
                            Range("A" & r).EntireRow.Hidden = True
                        End If
                    End With
                Next r
            End With
        End If
        Range("a1").Select
    Next Sheet

    Worksheets(1).Activate

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic

    End Sub

也许我正在以完全错误的方式解决这个问题......

如果值为“”,最终目标是隐藏(或防止)A4:G350 之间的空白单元格打印这些行。

最佳答案

可以试试

 Sub HideBlanks()
    Dim Ws As Worksheet
    Dim Rw As Long
    Dim Arr As Variant, Rng As Range

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual

    For Each Ws In ThisWorkbook.Worksheets
        If Ws.Index > 1 Then
        Arr = Ws.Range("A4:A350").Value
          For Rw = LBound(Arr) To UBound(Arr)
            If Arr(Rw, 1) = "" Then
                If Rng Is Nothing Then
                Set Rng = Ws.Range("A" & Rw + 3)
                Else
                Set Rng = Union(Rng, Ws.Range("A" & Rw + 3))
                End If
             End If
            Next Rw
        End If
   If Not Rng Is Nothing Then Rng.EntireRow.Hidden = True
   Set Rng = Nothing
   Next Ws

    Application.ScreenUpdating = True
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic

    End Sub

关于excel - 我怎样才能加快循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54771923/

相关文章:

r - 基于 R 中的序列高效计算总和

类和属性选择器之间的 CSS 性能

excel - 从 Windows 服务应用程序运行 Excel 宏时出错

vba - 删除某些时间 VBA

vba - 如何删除找到的文件 FSO 的扩展名?

vba - 比较 2 张纸中的数据并找出不匹配的地方

vba - VBA:变体数据类型的意外行为

performance - 跟踪 ORM 性能

excel - 在数字较大的列中查找重复项(作为文本)

vba - 循环遍历所有图表并更改标签格式vba