+ Reply to Thread
Results 1 to 3 of 3

Macro keeps running even after the file that contains it has been closed

Hybrid View

  1. #1
    Registered User
    Join Date
    08-27-2013
    Location
    UK
    MS-Off Ver
    Excel 20010
    Posts
    25

    Macro keeps running even after the file that contains it has been closed

    I'm having a bit of a weird issue with a particular spreadsheet. It's used by a lot of people so I've tried to get it to close down after 10 minutes of inactivity, as well as do a couple of other little things (mainly remove any filters that have been applied) before it closes.

    The trouble I'm having is that the timed inactivity loop seems to remain active even after the spreadsheet has closed. When the loop runs out the macro manages to reload the spreadsheet but then produces an error because the variable is blank (presumably since the spreadsheet has been closed).

    I can't work out what's going on. Here's the code in ThisWorkbook:

    Private Sub Workbook_Open()
        Call SetTimer
        ' Set variable title equal to exact application window title
          title1 = Application.Caption
        End Sub
    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        If Sheets("INVOICE LOG").FilterMode = True Then
        Sheets("INVOICE LOG").ShowAllData: End If
        ActiveWindow.Zoom = 100
        Call StopTimer
        End
    End Sub
    Private Sub Workbook_SheetCalculate(ByVal Sh As Object)
        Call StopTimer
        Call SetTimer
    End Sub
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, _
      ByVal Target As Excel.Range)
        Call StopTimer
        Call SetTimer
    End Sub
    and here's what's in a module:

    Option Explicit
    Dim DownTime As Date
    Public title1 As String
    
    Sub SetTimer()
        DownTime = Now + TimeValue("00:10:00")
        Application.OnTime EarliestTime:=DownTime, _
          Procedure:="ShutDown", Schedule:=True
          End Sub
          
    Sub StopTimer()
        On Error Resume Next
        Application.OnTime EarliestTime:=DownTime, _
          Procedure:="ShutDown", Schedule:=False
     End Sub
    Sub ShutDown()
        'Application.DisplayAlerts = False
          ' Activate Microsoft Excel again
          AppActivate title1
     
     Dim AckTime As Integer, InfoBox As Object
        Set InfoBox = CreateObject("WScript.Shell")
        'Set the message box to close after about 10 seconds or so
        AckTime = 10
        If InfoBox.Popup("Export Log will close WITHOUT SAVING in 10 seconds unless you click OK!", _
        AckTime, "Warning!", 0) = vbOK Then
            DownTime = Now + TimeValue("00:10:00")
        Application.OnTime EarliestTime:=DownTime, _
          Procedure:="ShutDown", Schedule:=True
                Exit Sub
        Else: End If
    
        With ThisWorkbook
            .Saved = True
            .Close
        End With
    
    End Sub
    Any thoughts would be appreciated. Thanks!
    Last edited by davepoth; 11-19-2014 at 09:06 AM.

  2. #2
    Forum Guru karedog's Avatar
    Join Date
    10-03-2014
    Location
    Indonesia
    MS-Off Ver
    2003
    Posts
    2,971

    Re: Macro keeps running even after the file that contains it has been closed

    Hi,

    In Sub ShutDown(), you must terminated the running timer first, before you launch another timer. So, just add "Call StopTimer" before "AckTime = 10" line.

    Regards
    1. I care dog
    2. I am a loop maniac
    3. Forum rules link : Click here
    3.33. Don't forget to mark the thread as solved, this is important

  3. #3
    Registered User
    Join Date
    08-27-2013
    Location
    UK
    MS-Off Ver
    Excel 20010
    Posts
    25

    Re: Macro keeps running even after the file that contains it has been closed

    Thank you! I'll give that a go, but it certainly looks right now.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] BeforeClose event not running/working when file is closed through VBA in another file
    By jaimelwilson in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-27-2014, 03:43 AM
  2. [SOLVED] stop macro running when workbook has closed
    By nickymac in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 01-07-2013, 12:25 PM
  3. [SOLVED] Run macro x no. of times if file closed for x no. of days.
    By JonathanP in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 11-06-2012, 05:44 AM
  4. Accessing a closed xls file from a macro
    By Seapeace in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-27-2008, 09:11 AM
  5. Replies: 5
    Last Post: 10-25-2005, 01:05 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1