vba Application.ScreenUpdating 不会更改为 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18422346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 22:57:56  来源:igfitidea点击:

Application.ScreenUpdating won't change to false

excelvbaexcel-vba

提问by Chris Merola

I'm pretty new to VBA, but I've found my way around by reading through these forums. I've never had a problem with Application.ScreenUpdating not working and after researching online, I can't find an answer to my problem.

我对 VBA 还很陌生,但我通过阅读这些论坛找到了自己的方法。我从来没有遇到过 Application.ScreenUpdating 不工作的问题,在网上研究之后,我找不到我的问题的答案。

I've created a Daily Checklist application in excel that gives a month view in the Daily Checklist. The tasks line up in rows and you simply select yes, no or N/a, for that task for the day on the calendar. You can save the checklist and it copies the information to a data sheet in a separate tab. Everything works perfectly except when saving the information the Application.Screenupdating isn't working and the user ends up seeing the flickering and jumping back and forth between the data sheet and the calendar view.

我在 excel 中创建了一个 Daily Checklist 应用程序,它在 Daily Checklist 中提供了一个月视图。任务按行排列,您只需为日历上当天的任务选择是、否或不适用。您可以保存核对清单,并将信息复制到单独选项卡中的数据表中。一切正常,除非在保存信息时 Application.Screenupdating 不起作用,并且用户最终会看到数据表和日历视图之间的闪烁和来回跳跃。

Any ideas or guidance on why Application.ScreenUpdating is not changing to false? I've tried moving it around in different areas, but nothing seems to work.

关于为什么 Application.ScreenUpdating 没有更改为 false 的任何想法或指导?我试过在不同的区域移动它,但似乎没有任何效果。

Here's the Save checklist sub:

这是保存清单子项:

Sub Save_Checklist()

Dim Checklist_Date
Dim Completed As Long
Dim Left_to_Complete As Long
Dim Database_Date As Range
Dim Database_Row As Long
Dim bScrUpdate

bScrUpdate = Application.ScreenUpdating
If bScrUpdate = True Then Application.ScreenUpdating = False

Worksheets("Database").Unprotect Password:="youngC"

If MsgBox("This button will save this month's checklist data into the database. All previous information will be overwritten. Would you like to continue?", vbYesNoCancel, "Reset the Calender") = vbYes Then

Checklist_Date = Worksheets("Daily Checklist").Range("E4").Value
Add_to = Worksheets("Daily Checklist").Range("AL1").Value
Range("E55").Select
ActiveCell.Resize(2, Add_to).Select
Selection.Copy

    On Error Resume Next
    With Worksheets("Database").Activate
        Range("A1:A366").Select
        Selection.Find(What:=Checklist_Date, After:=ActiveCell, LookIn:=xlValues, LookAt:= _
            xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, MatchCase:=False _
            , SearchFormat:=False).Select
    On Error GoTo 0
        If Not Database_Date Is Nothing Then Application.Goto Database_Date, True
    End With

Selection.Offset(0, 1).PasteSpecial Paste:=xlPasteValues, Operation:=xlNone, SkipBlanks _
    :=False, Transpose:=True


Worksheets("Database").Protect Password:="youngC"

Else
    MsgBox ("Please be sure all information is correct before saving it.")
End If

If Not Application.ScreenUpdating = bScrUpdate _
    Then Application.ScreenUpdating = bScrUpdate

End Sub

This is the code I have running upon the workbook opening (I have grouping in the "Add Tasks to Month's Calendars" sheet):

这是我在工作簿打开时运行的代码(我在“将任务添加到月历”表中进行了分组):

Private Sub Workbook_Open()
    Worksheets("Main Menu").Activate
    With Worksheets("Add Tasks to Month's Calendars")
    .EnableOutlining = True
    .Protect Password:="youngC", _
    Contents:=True, UserInterfaceOnly:=True
    End With
End Sub

Any help at all would be greatly appreciated.

任何帮助都将不胜感激。

Thanks, Chris

谢谢,克里斯

回答by Graham Anderson

Personally I would just set the screenupdating property to false without checking it, once the VBA has finished running the screen will update as normal.

就我个人而言,我只是将 screenupdating 属性设置为 false 而不检查它,一旦 VBA 完成运行,屏幕将正常更新。

Also I think that the problem is that the second peice of code that runs when you open the worksheet does not turn screenupdating to false so you will see all the movements you described. Excel doesn't remember that the screenupdating was set to false by a previous macro and even if it did the code you have turns it back on anyway. You should be able to solve this by setting screenupdating to false on your first line, the modification is shown below.

此外,我认为问题在于,当您打开工作表时运行的第二段代码不会将 screenupdating 变为 false,因此您将看到您描述的所有动作。Excel 不记得之前的宏将 screenupdating 设置为 false,即使它执行了您所拥有的代码,无论如何还是将其重新打开。您应该可以通过在第一行将 screenupdating 设置为 false 来解决此问题,修改如下所示。

Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Worksheets("Main Menu").Activate
    With Worksheets("Add Tasks to Month's Calendars")
    .EnableOutlining = True
    .Protect Password:="youngC", _
    Contents:=True, UserInterfaceOnly:=True
    End With
End Sub

回答by AndrewK

If you print to debug during run the state of it, it will be false, but when you are in debug mode, then it will be always true as your code is in pause mode and this switches back to true automatically until you run it again in "fast" mode

如果您在运行状态期间打印以进行调试,它将为 false,但是当您处于调试模式时,它将始终为 true,因为您的代码处于暂停模式,并且会自动切换回 true,直到您再次运行它在“快速”模式下