A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.
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