Quantcast
Channel: Active questions tagged excel - Stack Overflow
Viewing all articles
Browse latest Browse all 88854

Trying to protect a workbook that has multiple users from users 2, 3, etc

$
0
0

I'm working on some VBA code that will automatically protect the workbook from users that access it after the first initial user. My department recently switched to a cloud based system and this code is intended for the our excel masterfile that keeps track of all of the bids we send to clients. The issue being, multiple people are accessing this file with no idea that anybody else is in the file and saving their changes. When there are multiple people saving on the same file it causes a "CONFLICT" version of the file to be created. Hence why I want to lock everyone out except for one person. I've pasted my code below. I'm pretty new to VBA and a novice at coding in general. I'm hoping someone can provide some guidance on this project. Thanks!

Private Sub Workbook_Open()
Dim ws As Worksheet
Dim User As Variant
User = ThisWorkbook.UserStatus
If Range("O2").Text = "" Then
    Set Range("O2").Value = User
Else
    ws.Protect Password:="Password1", UserInterfaceOnly:=True
    MsgBox (User & " is using the workbook right now. If you need a proposal please email "& User)
End If

End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)

Dim User As Variant
User = ThisWorkbook.UserStatus

On Error Resume Next

If User = Sheets("BidSheet2020").Range("O2").Text Then

    Sheets("BidSheet2020").Range("O2").Clear
Else
   ThisWorkbook.Saved = True
End If

End Sub


Viewing all articles
Browse latest Browse all 88854