I have created an xlam file which is saved on the shared drive and a dozen of people use it now. However, I'm not able to save any update I make to any of my code as it opens the file in read only mode. Is there a way I could check for the USERNAME and if it does not match then open the xlam file in read only mode?
I did test the below code but it does not work.
I did test the below code but it does not work.
VBA Code:
Private Sub Workbook_Open()
OpenInReadOnlyNew
End Sub
Sub OpenInReadOnlyNew()
' Replace "TargetUserName" with the username you want to match against
Dim targetUserName As String
targetUserName = "ABC123" 'in lower case
' Get the current username
Dim currentUserName As String
currentUserName = Environ("USERNAME")
' Check if the current user name matches the target user name
If LCase(currentUserName) <> targetUserName Then
ActiveWorkbook.ChangeFileAccess Mode:=xlReadOnly
'MsgBox ("Opened in read only mode")
End If
End Sub