I'm testing some code, which should copy data to a global tracker whenever the user finishes their work and closes it. It's working fine when I test it with a button but when I use it with BeforeClose event the destination Excel opens blank with no ribbon, menu bar, no date is pasted in and there is some strange line visible across the window (see attachment). It's random, sometimes it works normally, sometimes it doesn't. Would anyone know why it's happening?
That's my code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Dim path As String
Dim wb As Workbook
Cancel = False
path = ThisWorkbook.path & "\Test.xlsx"
Range("A1:F10").Copy
On Error GoTo Handler
Workbooks.Open (path)
On Error GoTo 0
Set wb = Workbooks("Test")
Worksheets("Sheet1").Range("A1").PasteSpecial Paste:=xlPasteValues
Application.CutCopyMode = False
'wb.Close False
MsgBox "OK"
Exit Sub
Handler:
MsgBox "Someone else is saving their data at the moment."& vbNewLine & _
"Please try in a few seconds"
Cancel = True
End Sub