Link to home
Start Free TrialLog in
Avatar of LMG09
LMG09

asked on

How to lock an open excel spreadsheet

Hi - We have an excel spreadsheet located within a shared folder on a Windows server. 3 users will access and update this spreadsheet at various times throughout the day. When one user is accessing the spreadsheet, we would like any other users who try to access the spreadsheet to be told that it's in use and to try again later. We do not want the spreadsheet to be opened in read only mode. Is this possible?
Avatar of Ajay Chanana
Ajay Chanana
Flag of India image

You can use sharepoint as solution to it where multi user access  the file.

Other way you can check below  link.

https://www.youtube.com/watch?v=yzcyEKgj9hg
Avatar of LMG09
LMG09

ASKER

Ajay - Thanks for your prompt response.

Unfortunately, we do not want multiple users to access the spreadsheet at the same time. When one user accesses the spreadsheet, we need to prevent other users from accessing it at the same time (not even read only mode). Is this possible?
This code will close an inactive workbook


''// In the Workbook module

Private Sub Workbook_Open()
    EndTime = Now + TimeValue("00:10:00")
    RunTime
End Sub

Open in new window

This resets the timer if a change in the workbook occurs

Worksheet Change Event

Private Sub Worksheet_Change(ByVal Target As Range)
    If EndTime Then
        Application.OnTime _
          EarliestTime:=EndTime, _
          Procedure:="CloseWB", _
          Schedule:=False
        EndTime = Empty
    End If
    EndTime = Now + TimeValue("00:10:00")
    RunTime
End Sub

Open in new window


If no changes have occurred then this runs
in a Standard Module
Public EndTime
Sub RunTime()
    Application.OnTime _
      EarliestTime:=EndTime, _
      Procedure:="CloseWB", _
      Schedule:=True
End Sub

Sub CloseWB()
    Application.DisplayAlerts = False
    With ThisWorkbook
        .Saved = True
        .Close
    End With
End Sub

Open in new window

If you need to check for changes in all sheets of the workbook then use the Workbook_SheetChange event
Avatar of LMG09

ASKER

Thanks Roy for your prompt response. Unfortunately, It doesn't seem to answer my question (unless I'm totally missing the point :-))

If you had a spread sheet open and I tried to open that spread sheet as well, by default I would be able to open that spreadsheet in read only mode. I need a way of denying me access to the spreadsheet altogether whilst you still have that spread sheet open. When you close the spreadsheet, I'm then able to access the spreadsheet as normal. I hope this makes sense?

Regards
ASKER CERTIFIED SOLUTION
Avatar of Ajay Chanana
Ajay Chanana
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of LMG09

ASKER

Ajay - I agree with your comments but I thought I would put it out to the experts - just in case it was possible
I have code that would detect if the workbook is open that works  if the users are opening the file by means of VBA. It doesn't seem to work when opening from Windows Explorer, but I've not tested using a shared folder. I can post the code if you want to try it.

The code I posted earlier was intended to close the workbook if it remained idle,  reducing the need for other users to wait unnecessarily.
Avatar of LMG09

ASKER

Thanks for all of your input