picklefactory
Well-known Member
- Joined
- Jan 28, 2005
- Messages
- 508
- Office Version
- 365
- Platform
- Windows
Hi folks, trying to fix a bug in an old WB and managing to make it worse
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
And in a Module
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