优化VBA代码-基于单元格值的行高

时间:2019-03-22 12:18:28

标签: excel vba

我正在尝试根据单元格值调整行高。该操作必须遍历约700行的过滤数据。

下面的代码可以工作,但是需要2-3分钟才能完成操作,这太长了。

我是否有可能无循环执行此操作? 或者,请告诉我是否还有其他措施可以缩短手术时间。

非常感谢您的帮助!

irb -I .
irb(main):001:0> require 'config/boot'
=> true
irb(main):002:0> require 'lib/repositories/account_repository'
=> true
irb(main):003:0> rep = AccountRepository.new
Traceback (most recent call last):
        6: from /home/mvalitov/.asdf/installs/ruby/2.5.1/bin/irb:11:in `<main>'
        5: from (irb):3
        4: from (irb):3:in `new'
        3: from /home/mvalitov/.asdf/installs/ruby/2.5.1/lib/ruby/gems/2.5.0/gems/hanami-model-1.3.2/lib/hanami/repository.rb:420:in `initialize'
        2: from /home/mvalitov/.asdf/installs/ruby/2.5.1/lib/ruby/gems/2.5.0/gems/rom-repository-1.4.0/lib/rom/repository/root.rb:62:in `initialize'
        1: from /home/mvalitov/.asdf/installs/ruby/2.5.1/lib/ruby/gems/2.5.0/gems/rom-3.3.3/lib/rom/registry.rb:30:in `fetch'
ArgumentError (key cannot be nil)

2 个答案:

答案 0 :(得分:0)

您可以尝试:

Option Explicit

Sub test()

    Dim i As Long, arr As Variant

    With ThisWorkbook.Worksheets("Sheet1")

        arr = .Range("AJ6:AJ700")

        For i = LBound(arr) To UBound(arr)

            If arr(i, 1) > 0 Then
                .Rows(i + 5).EntireRow.rowheight = arr(i, 1)
            End If

        Next i

    End With

End Sub

答案 1 :(得分:-1)

假设大多数处理是在设置行高时进行的,并且假设要将其设置为最大值,则该函数将变为:

Sub rowheight()
    Dim hgt As Integer
    Dim WorkRng As Range

    Application.ScreenUpdating = False

    Set WorkRng = Range("AJ6:AJ700")

    hgt = 0
    For Each c In WorkRng.SpecialCells(xlCellTypeVisible)
        If c.Value > hgt Then
            hgt = c.Value
        End If
    Next c
    WorkRng.EntireRow.rowheight = hgt

    Application.ScreenUpdating = True
End Sub