Automatically close Workbook after inactivity

前端 未结 2 1690
臣服心动
臣服心动 2021-01-11 19:34

I created a Macro that closes the WB after some time of inactivity. It works perfect if I manually open the file, but if I use another macro from a different WB to open the

相关标签:
2条回答
  • 2021-01-11 19:54

    try adding a stop statement to your workbook_open to test if the event is even being triggered

    Private Sub Workbook_Open()
      start_Countdown
      Stop
    End Sub
    

    this would be a brute force way the to run the open Event from the Calling Workbook.

    Application.Run(ActiveWorkbook.name & "!Workbook_Open")

    add this just after you open the Workbook.

    0 讨论(0)
  • 2021-01-11 19:55

    As Susilo pointed out in the comments, the issue must be something else than the auto-close code itself, since it works. That "something else" then, is probably the Answer_Quote() code, which frankly is one big mess. I'd recommend the following:

    USE DUMMY CODE

    Try running a dummy macro (a macro that essentially does nothing but open the workbook that should auto-close after some inactivity) instead of Answer_Quote()to see if the problem persists. If it doesn't, then you know for sure that Answer_Quote() is causing the problem. Proceed then to code cleanup.

    CODE CLEANUP

    1) Set all objects, external file and sheet references to nothing upon exit.

    Optionally and thus less importantly, but to ease code maintenance and debugging, I'd also recommend:

    2) Use proper and consistent indentation

    3) Remove redundant lines of code

    For instance:

    If wBook Is Nothing Then 'Not open
            Set wBook = Nothing
    

    It is obviously pointless to set a reference to nothing if it is already nothing.

    4) Dimension all variables at the top, rather than throughout the code.

    5) Use Option explicit (if you don't already)

    TEST AUTO-CLOSE EXECUTION

    After code cleanup, test again. If the problem persists, try commenting out some of theAnswer_Quote() code and try again. Repeat this process until the auto-close execution works again and you can pinpoint the exact cause of the problem.

    0 讨论(0)
提交回复
热议问题