Never ending loop with OnTime

picklefactory

Well-known Member
Joined
Jan 28, 2005
Messages
508
Office Version
  1. 365
Platform
  1. Windows
Hi folks, trying to fix a bug in an old WB and managing to make it worse :rolleyes:
This WB has run for years with a bug that has been irritating me, but I've never found time to fix...... I'm trying now.... and failing miserably.
It's a WB shared across numerous machines, so I have an OnTime setup to close it down after a period of idleness, as nobody else would be able to use it if left open. It is a password protected WB and the users can only use it read only, they are only allowed to view it and not edit at all, apart from authorised staff.
I also have a function that records every time the WB is edited and by who, this is where it now drops into the OnTime loop, AFTER I correct the workbook.save error, by simply closing it. It now completes the loop in the Edit Log to find the next empty row, it adds the name of the person who is editing, then due to WorkbookSelectionChange, it drops into the OnTime functions and can't escape. The old error was clearly interrupting the infinite loop and hiding the underlying error.
The standard bug it's had forever is that I am not closing it correctly (Rank amateur), it tries to save/close but as it's read only obviously it can't, so gives an error. That error has just hidden another error for all this time. I have tried changing the VBA to simply close the WB instead of trying to save and close, which cured the long term error, but now it just drops into a never ending loop around the OnTime functions and can't find it's way out again.
Can any of you clever buggers spot where I'm being daft please? Please be kind, I'm only a humble spanner monkey and these things are hard for spanner monkeys.....
I'm posting the code as it was originally, with the long term error of trying to save when read only.
Thank you

In ThisWorkbook

VBA Code:
Private Sub Workbook_Open()
    Call SetTimer
End Sub
Private Sub Workbook_BeforeClose(Cancel As Boolean)
 
    
    Call EditLog
    
    ActiveWorkbook.Save
        
    
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 in a Module

VBA Code:
Dim DownTime As Date

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
    
    Call EditLog
    
    ActiveWorkbook.Save
        
    
    End Sub

Function LastAuthor()
LastAuthor = ActiveWorkbook.BuiltinDocumentProperties("Last Author")
End Function

Function LastModified() As Date
   LastModified = ActiveWorkbook.BuiltinDocumentProperties("Last Save Time")
End Function

Sub EditLog()

    Worksheets("EDIT LOG").Visible = True
    Worksheets("EDIT LOG").Select
    Range("A2").Select
    Do While Not ActiveCell = ""
    ActiveCell.Offset(1, 0).Select
    Loop
    
ActiveCell.Value = LastAuthor()
ActiveCell.Offset(0, 1).Value = LastModified()
        
    Sheets(Format(Now, "mmmm")).Activate
    
    Worksheets("EDIT LOG").Visible = False

End Sub
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
You don't need to select anything in that code. Try:

VBA Code:
Sub EditLog()

    With Worksheets("EDIT LOG")
        .cells(.rows.count, "A").End(xlUp).Offset(1).Resize(, 2).Value = Array(LastAuthor(), LastModified())
    end with

    Sheets(Format(Now, "mmmm")).Activate

End Sub
 
Upvote 0
Solution
You don't need to select anything in that code. Try:

VBA Code:
Sub EditLog()

    With Worksheets("EDIT LOG")
        .cells(.rows.count, "A").End(xlUp).Offset(1).Resize(, 2).Value = Array(LastAuthor(), LastModified())
    end with

    Sheets(Format(Now, "mmmm")).Activate

End Sub
Yippee.... it works, of course. Who could doubt it?
You could at least pretend that you had to think about it a bit, and that it wasn't quite so easy :)

Thanks Rory, another success, much appreciated
 
Upvote 0

Forum statistics

Threads
1,223,936
Messages
6,175,500
Members
452,650
Latest member
Tinfish

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top