THE CURRENT EXCEL FILE IS LOCKED FOR EDITING CODE
In his application, he wanted the code to check the status of the file from MS Access instead of Excel, but I’ve modified the code so it works entirely in Excel.Īll you have to do is paste the example macro in the ThisWorkbook object in your VBA editor. Remo was looking for a way to check if a spreadsheet was opened by a user on his network so he could tell them to close it. Tutorial - Excel locked for editing What this macro doesĪ version of the example macro above was sent to me by a brilliant reader named Remo. Grab them below and you’ll be writing powerful macros in no time. That’s why we created our free VBA Developer Kit and our Big Book of Excel VBA Macros to supplement this tutorial. Make powerful macros with our free VBA Developer Kit Path & "\usage.log" For Input Access Read As # file1 Do While Not EOF ( file1 ) Line Input # file1, strLine Loop Close # file1 MsgBox "The file was locked by following user: " & strLine 'last line of file" End If End Sub Path & "\usage.log" For Append As # file1 Print # file1, Environ ( "USERNAME" ) & " at " & Now () Close # file1 Else 'if someone else has the file open, find out who Open ThisWorkbook. ReadOnly = True Then 'only add name to the usage log if the user has it locked Open ThisWorkbook. Path & "\usage.log" ' delete the file if it exists and it is possible End If On Error GoTo 0 ' break on errors End Sub Private Sub Workbook_Open () Dim file1 As Integer Dim strLine As String file1 = FreeFile If Not ActiveWorkbook. ReadOnly = True Then 'only try to delete the file if the user has it locked Kill ThisWorkbook. 'Place these macros in your "ThisWorkbook" object Private Sub Workbook_BeforeClose ( Cancel As Boolean ) On Error Resume Next ' ignore possible errors If Not ActiveWorkbook.