VBA Macro to check if worksheet is already opened by other user

richi5000

New Member
Joined
Nov 14, 2016
Messages
8
Hi,

in an Worksheet that is stored on a sharepoint I've implemented the following macro:

Code:
Private Sub Workbook_Open()
If Application.Version >= "16" Then
    ActiveWorkbook.LockServerFile
Else 'Under Excel 2016 version 
End If
End Sub


Reason for this: In Excel 2016 this file is opened as read-only. Thats why this macros checks for the version of Excel - and if its 16 or higher it enables editing automatically.


Problem: If an other user has the file already open, this produces an error.


Is there a macro command that I can include to check if the document is already opened by other user - so if that is true, it stops the loop and opens the file in read-only?

If not - how can I stop the macro in case of an error? (Result would be the same - it opens in read-only)!?
Thanks
Richie
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
Hi.

To catch error you can use:

Code:
On Error Resume Next
ActiveWorkbook.LockServerFile
If Err.Number <> 0 Then Exit Sub
Err.Clear
On Error Goto 0
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,304
Members
452,633
Latest member
DougMo

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