Introduction | Example | Tutorial | Applications | Comments
Introduction - Excel locked for editing
Use VBA to find out who has your Excel file “locked for editing.” There are few things more frustrating than trying to open a shared workbook on a network drive and discovering another user has it locked. Save this macro to your spreadsheet and it will tell you when someone else has your file opened and locked.
Most of the time, the “Notify” prompt that appears when the file is locked will automatically tell you who has the file open, but often when files are opened on a network, it gets confused and doesn’t know who has it open. This tutorial will show you a reliable alternative to finding out who has your file locked.
Example - Excel locked for editing
Create a log of who has your file open
'Place these macros in your "ThisWorkbook" object Private Sub Workbook_BeforeClose(Cancel As Boolean) On Error Resume Next ' ignore possible errors If Not ActiveWorkbook.ReadOnly = True Then 'only try to delete the file if the user has it locked Kill 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 add name to the usage log if the user has it locked Open ThisWorkbook.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.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
Write better macros in half the time
I see people struggling with Excel every day and I want to help. That's why I'm giving away my personal macro library for free. This powerful gift lets you automatically import all my macros directly into your spreadsheets with just one click.
Tutorial - Excel locked for editing
What this macro does
A version of the example macro above was sent to me by a brilliant wellsr.com reader named Remo. 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. 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.
Once you do that, save your workbook and close it.
When the next person opens it, the macro will check to see if the file is read-only. If it’s not, it means the user is locking the file, which prohibits other users from editing it.
If the user has the file locked, it will record the user’s username and the date/time into a file called
When another user tries to open the file and the file is read-only, they’ll still get the annoying “Notify” prompt, but then they’ll also get a helpful message reliably saying who has the file locked and when they locked it:
More about the usage.log file
The usage.log file is actually even more valuable than it sounds! Once the user locking your spreadsheet closes the file, the usage.log file is deleted.
That means you really don’t even have to open your spreadsheet to see if someone has your file locked. If you see the usage.log file, that means someone has your spreadsheet open.
You can just open the usage.log file and instantly see who has your workbook locked. Again, you don’t even have to open the spreadsheet to see!
Alternate way to check who has Excel file locked
This isn’t the only way to check who has your file open. There’s actually a clever way to do it using the Windows Management Instrumentation (WMI) and VBA FileSystemObject. This solution was sent to me by Philip Hanebeck:
Function Excel_File_in_use_by(FilePath As String) As String Dim strTempFile As String Dim iPos As Integer, iRetVal As Integer Dim objFSO As Object, objWMIService As Object, objFileSecuritySettings As Object, objSD As Object iPos = InStrRev(FilePath, "\") strTempFile = left(FilePath, iPos - 1) & "\~$" & Mid(FilePath, iPos + 1) Set objFSO = CreateObject("Scripting.FileSystemObject") If objFSO.FileExists(strTempFile) Then Set objWMIService = GetObject("winmgmts:") Set objFileSecuritySettings = objWMIService.Get("Win32_LogicalFileSecuritySetting='" & strTempFile & "'") iRetVal = objFileSecuritySettings.GetSecurityDescriptor(objSD) If iRetVal = 0 Then Excel_File_in_use_by = objSD.Owner.Name Else Excel_File_in_use_by = "unknown" End If Set objWMIService = Nothing Set objFileSecuritySettings = Nothing Set objSD = Nothing Else Excel_File_in_use_by = vbNullString End If Set objFSO = Nothing End Function
The nice thing about this function is that it doesn’t have to be placed in the workbook you’re interested in. You pass the function the path of the locked Excel workbook and it’ll look for the temporary
If you’re in charge of a shared schedule or KPI you track on a spreadsheet, it can be so frustrating to discover someone else locked you out of your own work. By adding the example macro in this tutorial to your spreadsheet, the log file it produces will at least tell you who you need to bug to regain access to your workbook!
If you liked this VBA tutorial, I hope you’ll shop my Excel Add-ins store to show your support for wellsr.com. Without revenue from great readers like you, it’s difficult to grow the site and reach more users.