ScreenUpdating = False无法正常工作

时间:2014-07-31 19:45:52

标签: excel-vba vba excel

我知道有很多关于这个主题的主题,比如不要使用" Select"或"激活"如果需要使用它,请将其设置为false。每次我使用"选择"我都将它设置为False或者"激活",但它仍然不起作用,请帮助!!

Sub Forecast()

    Application.ScreenUpdating = False
    Application.DisplayAlerts = False
    Application.DisplayStatusBar = True
    'Application.StatusBar = "Please be patient..."

    Dim Rating As Variant, sht As Worksheet, LastRecordRow As Long, i As Integer, LastRow As Long

    Rating = InputBox("Please Provide Weather Rating (Any Number Between 1 and 4)", "Input Needed")

    If Rating < 0 Or Rating > 4 Then
        MsgBox "Invalid Value, Please Enter A Valid Number! (1~4)", , "Alert!!!"
        Exit Sub
    Else

        For Each sht In ActiveWorkbook.Worksheets
        If sht.Name = "Forecast" Then
            sht.Range("a1") = ""
        Else
            LastRecordRow = sht.Range("A1").End(xlDown).Row
            sht.Range("I1:O1").EntireColumn.Delete
            Application.Run "ATPVBAEN.XLAM!Regress", sht.Range("$B$1", "$B" & LastRecordRow), _
            sht.Range("$C$1", "$C" & LastRecordRow), False, True, , sht.Range("$I$1") _
            , False, False, False, False, , False
            PVTotal = Application.WorksheetFunction.Sum(Range("B2", "B" & LastRecordRow))
            ImpTotal = Application.WorksheetFunction.Sum(Range("D2", "D" & LastRecordRow))
            sht.Range("B" & LastRecordRow + 1) = PVTotal
            sht.Range("D" & LastRecordRow + 1) = ImpTotal
            sht.Cells.EntireColumn.AutoFit
            sht.Range("A1").Select
        End If
        Next sht

        Worksheets("Forecast").Activate

        i = 1

        ActiveSheet.Range("B" & i + 2, Range("H" & i + 2).End(xlDown)).EntireRow.Delete

        Do While i <= ActiveWorkbook.Worksheets.Count
            RowForSum = Worksheets(i).Range("B1").End(xlDown).Row
            With ActiveSheet
                .Cells(i + 2, 2).Value = Worksheets(i).Name
                .Cells(i + 2, 3).Value = Worksheets(i).Range("J17")
                .Cells(i + 2, 4).Value = Worksheets(i).Range("J18")
                .Cells(i + 2, 5).Value = Rating
                .Cells(i + 2, 6).Value = ActiveSheet.Cells(i + 2, 3).Value + ActiveSheet.Cells(i + 2, 4) * Rating
                If Worksheets(i).Range("B183").Value = 0 Then
                    .Cells(i + 2, 7).Value = 0
                Else
                    .Cells(i + 2, 7).Value = Worksheets(i).Range("D" & RowForSum).Value / Worksheets(i).Range("B" & RowForSum).Value
                End If
                    .Cells(i + 2, 8).Value = ActiveSheet.Cells(i + 2, 6).Value * ActiveSheet.Cells(i + 2, 7)
            End With
            i = i + 1
        Loop

        LastRow = ActiveSheet.Range("B2").End(xlDown).Row

        a = Application.WorksheetFunction.Sum(ActiveSheet.Range("F3", "F" & LastRow))
        b = Application.WorksheetFunction.Sum(ActiveSheet.Range("H3", "H" & LastRow))

        With ActiveSheet
            .Range("F" & LastRow + 1).Value = a
            .Range("F" & LastRow + 1).Offset(0, -4).Value = "Total"
            .Range("H" & LastRow + 1).Value = b
            .Range("A1").Select
            .Cells.EntireColumn.AutoFit
        End With

        Dim rng2 As Range
        For Each rng2 In ActiveSheet.Range("B2", Range("B2").End(xlDown))
            If rng2 = "Forecast" Then
                rng2.EntireRow.Delete
            Else
                If rng2 = "Total" Then rng2.EntireRow.Font.Bold = True
            End If
        Next
    End If

    Application.ScreenUpdating = True
    'Application.StatusBar = False
    Application.DisplayAlerts = True

End Sub

2 个答案:

答案 0 :(得分:2)

我有类似的问题。使用Application.ScreenUpdating = False的工作簿在Excel 2016中变得很慢,并且在运行更新工作表的VBA时闪烁。

使用消息框和Debug.Print语句,我将其跟踪到一个循环,使用以下代码更新单元格内容:

Dim rowCounter As Integer
For rowCounter = 1 To numberOfEmployees
    'Do some work
    If (someCondition) Then
        startCell.offset(rowCounter).Value = ""
    Else 
        startCell.offset(rowCounter).Value = "something else"
    End If
Next rowCounter

如果我更换了以下行

startCell.offset(rowCounter).Value = ""

的任何一个

startCell.offset(rowCounter).Value = "anything except empty"

startCell.offset(rowCounter).ClearContents

然后闪烁停止,执行循环的时间从几秒钟变为远小于1秒。

我不确定为什么会这样。

因此,如果循环中的任何单元格返回空字符串,则可以尝试使用这些行中的任何一行。

答案 1 :(得分:0)

不要把它禁用和启用放在同一个程序/模块/功能中。 根据需要保留 Application.ScreenUpdating = False 并将 Application.ScreenUpdating = true 置于此过程/模块/函数之外。对我来说,这是唯一真正有效的方法。