Hi Harry
Thanks for your time. I have tried the latter, but the problem is that the rest of the code DOES execute, so putting it in the timeout means it runs twice (albiet when called from the before save macro, the workbook doesn't open), and this causes other problems.
Re your first idea about it loosing the variable being a possibility, I have tried removing the variable which gives the full filepath and directly referring to the filename & path, but the same thing happens.
The macro which is called from before save (that runs perfectly when executed via the user clicking save) is as follows:
Sub ShutDown()
ThisWorkbook.Activate
ActiveWorkbook.Worksheets(1).Activate
ActiveSheet.Unprotect Password:="TP"
Application.ScreenUpdating = False
Range("A42:AJ48").Select
Application.CutCopyMode = False
Selection.Copy
Range("A1:AJ7").Select
Selection.PasteSpecial Paste:=xlPasteFormats, Operation:=xlNone, _
SkipBlanks:=False, Transpose:=False
Application.ScreenUpdating = False
ActiveSheet.Protect Password:="TP", DrawingObjects:=False, Contents:=True, Scenarios:= _
True, AllowFormattingCells:=True, AllowFormattingColumns:=True, AllowFormattingRows:=True
ActiveSheet.EnableSelection = xlNoRestrictions
Range("BB37").Select
Selection.Copy
Application.ScreenUpdating = True
Dim myFile As String, myFolder As String
myFolder = Range("Filepath")
myFile = myFolder & Range("Filename")
If Not IsFolderExists(myFolder) Then
CreateObject("Scripting.FileSystemObject").CreateFolder myFolder
End If
If Not IsFileExists(myFile) Then
MsgBox "The folder path & file " & myFile & " could not be found and the global TP cannot be updated." & vbCrLf _
& "Please inform the administrator"
UserForm1.Hide
Exit Sub
End If
If ActiveWorkbook.Saved = False And ActiveWorkbook.ReadOnly = False Then
ActiveWorkbook.Save
UserForm1.Show
UserForm1.Repaint
Application.DisplayAlerts = False
ActiveWorkbook.Worksheets(1).Activate
Range("A3:AJ7").Select
Selection.Copy
Dim CurrentGlobal As String
CurrentGlobal = Replace(Range("Filename"), "\", "")
Application.Workbooks.Open (Range("Fullname").Text), WriteResPassword:="GTP"
Application.Windows(CurrentGlobal).Activate
If ActiveWorkbook.ReadOnly = True Then
UserForm1.Hide
MsgBox "Another user has the global TP open. Could not update- please click save again to update the global TP"
ActiveWorkbook.Close
Application.DisplayAlerts = True
Exit Sub
End If
ActiveWorkbook.Worksheets(1).Activate
Range("A3").Select
ActiveWorkbook.Worksheets(1).Paste
'PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
Range("A1").Select
ActiveWorkbook.Save
ActiveWindow.Close
Application.DisplayAlerts = True
Range("B3").Select
UserForm1.Hide
End If
Application.Quit
End Sub
The macro which executes on workbook open is:
Private Sub Workbook_Open()
Call SetTime
End Sub
The SetTime macro is:
Dim DownTime As Date
Sub SetTime()
DownTime = Now + TimeValue("00:05:00")
Application.OnTime DownTime, "ShutDown"
End Sub
The ShutDown macro that it calls after five minutes is:
Sub ShutDown()
ThisWorkbook.Save
Application.Quit
End Sub
As the ShutDown macro saves the workbook it should and does trigger the ToGlobal macro, but the line opening the other workbook doesn't do anything.