Share via

Requesting Code

Anonymous
2024-07-30T16:01:53+00:00

Where to get the code to lock cells once Dara has been entered into them and the file is saved so the next user cannot edit any previously entered data. And, how is the code applied to excel spreadsheet.

* Changed to a question

Microsoft 365 and Office | Excel | Other | Windows

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

1 answer

Sort by: Most helpful
  1. Anonymous
    2024-07-31T07:20:36+00:00

    Hi,Deborah East-Whitfield

    Welcome to the Microsoft Community.

    Understanding that you want to get the appropriate code to realize the function of saving the file after entering data so that the next user cannot edit the entered data, you can refer to the following:  Open an Excel workbook.Press Alt + F11 to open the VBA editor.Select “Insert” > “Module” to insert a new module and paste the following code.You can change the value of the Password variable to your desired password.Private Password As StringPrivate IsProtected As BooleanPrivate Sub Workbook_Open()Password = "YourPassword" ' Set your desired passwordIsProtected = False' Re-protect the worksheet if it was previously protectedIf IsProtected ThenProtectSheetEnd IfEnd SubPrivate Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)ProtectSheetEnd SubSub ProtectSheet()ActiveSheet.Protect Password:=Password, UserInterfaceOnly:=TrueIsProtected = TrueEnd SubSub UnprotectSheet()Dim enteredPassword As StringenteredPassword = InputBox("Please enter your password to unprotect the worksheet:", " password")If enteredPassword = Password ThenActiveSheet.Unprotect Password:=PasswordIsProtected = FalseElseMsgBox "Wrong password to unprotect", vbExclamation, "Error"End IfEnd Sub In summary, this code protects worksheets from unauthorized modification by automatically running the ProtectSheet procedure when a workbook is opened and saved. Users need to know the correct password and unprotect the sheet by running the UnprotectSheet procedure before they can edit the sheet.

    Best regards

    Zoro-MSFT | Microsoft Community Support Specialist

    Was this answer helpful?

    0 comments No comments