Windows Excel更新后VBA变慢

时间:2019-02-25 11:25:06

标签: excel vba

我写下了一个用于在不到1秒的时间内运行的宏。但是,在Windows / Excel更新后,宏现在需要5分钟才能完成!

可以请教吗?

在我的宏下面

Sub B_Palett_Weight_Check()        
    Dim ws As Worksheet
    Dim Vws As Worksheet

    Set Vws = ThisWorkbook.Sheets("Variables")

    Sheets("Sheet1").Select

    Application.ScreenUpdating = False
    Application.EnableEvents = False
    Application.Calculation = xlCalculationManual
    Application.PrintCommunication = False
    ActiveSheet.DisplayPageBreaks = False
    Application.DecimalSeparator = ","
    Dim target As Variant
    Dim r_AH As Range
    Dim n As Long
    Dim i As Long

    For Each ws In ActiveWorkbook.Worksheets
        If ws.Name <> "Variables" Then
            ' For the target value for each worksheet
            target = Application.WorksheetFunction.VLookup(ws.Name, Vws.Range("A1:C10"), 2, False)


            'Find the number of cells in column AH
            n = ws.Range(ws.Range("AH3"), ws.Range("AH3").End(xlDown)).Rows.Count
            Set r_AH = ws.Range("AH3")

            For i = 1 To n
            ' Go down the column AH
                If r_AH.Cells(i, 1).Value >= target Then
                    r_AH.Cells(i, 1).Interior.Color = vbRed
                Else
                    r_AH.Cells(i, 1).Interior.Color = vbWhite
                End If
            Next i
        End If
    Next ws
End Sub

1 个答案:

答案 0 :(得分:1)

我认为问题出在n,因为它的计算方式一直到工作表的最后一行,在较新的版本中为1048576而不是65536。如果从2003更新到更高版本,则for循环已经变得更大。

我认为您想像这样计算n:

	<div class="floating_text <?php echo $this->position ?>">

通过使用xlUp,n将是AH列中的最后已使用行。