间歇性VBA宏错误

时间:2016-05-17 14:06:42

标签: excel vba excel-vba

我已经编写了以下代码来循环浏览我的工作表,作为一种在销售部门中使用的幻灯片。当我在调试模式中进行操作时,代码工作正常,但是当我运行宏时,它只能间歇性地工作,偶尔可以选择工作表而无需重新激活屏幕更新应用程序功能。

这是我到目前为止创建的代码:

Sub Runshow()

Dim ws As Worksheet

On Error GoTo exit_
Application.EnableCancelKey = xlErrorHandler

For Each ws In ThisWorkbook.Worksheets
    ws.Protect
Next

Application.DisplayFullScreen = True
Application.DisplayFormulaBar = False
ActiveWindow.DisplayWorkbookTabs = False
ActiveWindow.DisplayHeadings = False
ActiveWindow.DisplayGridlines = False
ActiveWindow.DisplayHorizontalScrollBar = False
ActiveWindow.DisplayVerticalScrollBar = False
Application.Calculation = xlManual

Let y = 0
Do Until y = 80

    Application.ScreenUpdating = False

    Workbooks.Open("c:\users\admin\downloads\crm.xlsx").Activate
    Application.Calculate

    ActiveWorkbook.Close savechanges = False
    Application.ScreenUpdating = True
    ThisWorkbook.Activate

    Let x = 0
    Do Until x = 23

        For Each ws In ActiveWorkbook.Worksheets
            ws.Select
            Application.Wait (Now + TimeValue("00:00:10"))
            x = x + 1
        Next

    Loop

    y = y + 1
Loop

exit_:

For Each ws In ThisWorkbook.Worksheets
    ws.Unprotect
Next

Application.DisplayFullScreen = False
Application.DisplayFormulaBar = True
ActiveWindow.DisplayWorkbookTabs = True
ActiveWindow.DisplayGridlines = True
ActiveWindow.DisplayHorizontalScrollBar = True
ActiveWindow.DisplayVerticalScrollBar = True
Application.Calculation = xlAutomatic

End Sub

1 个答案:

答案 0 :(得分:0)

我整理了一些类似的简单代码,效果很好。你可以从这里建立 - 如果有任何你不理解的事情,可以问任何问题。

Sub Slideshow()
    Dim ws As Worksheet

    PrepareView True

    For Each ws In ThisWorkbook.Worksheets
        ws.Activate
        Application.Wait (Now + TimeValue("00:00:10"))
    Next ws

    PrepareView False
End Sub

Function PrepareView(status As Boolean)
    If status = True Then
        ActiveWindow.DisplayWorkbookTabs = False
        ActiveWindow.DisplayHeadings = False
        ActiveWindow.DisplayGridlines = False
        ActiveWindow.DisplayHorizontalScrollBar = False
        ActiveWindow.DisplayVerticalScrollBar = False
    ElseIf status = False Then
        Application.DisplayFullScreen = False
        Application.DisplayFormulaBar = True
        ActiveWindow.DisplayWorkbookTabs = True
        ActiveWindow.DisplayGridlines = True
        ActiveWindow.DisplayHorizontalScrollBar = True
        ActiveWindow.DisplayVerticalScrollBar = True
    End If
End Function