Identify if shared workbook is locked by another user before running macro

JustJohn91

New Member
Joined
Apr 4, 2016
Messages
11
Hi All,

Ive searched and searched and cant find an answer to my query. If it has been answered already please point me in the right direction.

I have a shared excel workbook where multiple staff could be in at the same time. Staff input and update data via a userform. To populate the excel sheet with the data from the userform, the workbook is saved (To update to latest version of data), the data is populated and the workbook is saved again. The entire process takes 2-3 seconds. However, if a staff member has another excel workbook open, this time can increase.

If User A presses the save button, and User B the presses the save button before User A's process has completed, the sheet is locked for editing and User B's data isnt entered and is lost.

Is there a way to identify that the sheet is locked for editing and to wait and check again in 5 seconds?

Thanks In Advance,
John
 

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
This will ask the user to try again. It gives them a chance to call the offending co worker in question, have them shut down the workbook and wait for windows to do it's thing unlockign the file. Which will take more than 5 seconds.

VBA Code:
Function FileLocked(ByVal strFileName As String) As Boolean
    Dim Response As VbMsgBoxResult
   
CheckFile:
    On Error Resume Next
' If the file is already opened by another process,
' and the specified type of access is not allowed,
' the Open operation fails and an error occurs.
    Open strFileName For Binary Access Read Write Lock Read Write As #1
    Close #1
    FileLocked = CBool(Err.Number <> 0)
    On Error GoTo 0
    If FileLocked Then
'read / write file in use
        Response = MsgBox("File Is Open For Editing By Another User." & Chr(10) & _
        "Do You Want To Try Again?", 37, "File In Use")
        If Response = vbRetry Then GoTo CheckFile
    End If
End Function

If FileLocked(strDestWorkbookPath & "\" & strDestWorkbookName) Then
        End
End If
 
Upvote 0

Forum statistics

Threads
1,224,813
Messages
6,181,116
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top