Forum Discussion

Moulahoum1991's avatar
Moulahoum1991
Copper Contributor
Jul 08, 2024
Solved

Automatically protect cells in a file once the file is saved and closed by a user

Hello fellow Excel Users,   I am not to automating tasks in Excel and would like your support with the following :    Description: I have am excel spreadsheet and I am sharing through a sharepoin...
  • HansVogelaar's avatar
    HansVogelaar
    Jul 09, 2024

    Moulahoum1991 

    Is this better?

    Private Sub Workbook_BeforeClose(Cancel As Boolean)
        On Error Resume Next
        With Worksheets("My Sheet")
            .Unprotect Password:="OurPassword"
            .Cells.SpecialCells(xlCellTypeFormulas).Locked = True
            .Cells.SpecialCells(xlCellTypeConstants).Locked = True
            .Protect Password:="OurPassword"
        End With
    End Sub

Resources