Forum Discussion
Christina333
May 17, 2021Copper Contributor
Lock cell without protecting worksheet
Hi all, Hope everyone is doing well there. I would like to seek advise from experts here on how to 'lock certain cells without protecting the worksheet.' Is it possible to do so? My compa...
HansVogelaar
May 17, 2021MVP
You could use VBA code to prevent some cells from being edited, but that only helps with accidental edits. Someone who knows a bit about Excel can easily circumvent it by either opening the workbook without allowing macros, or by temporarily disabling events.
Here is what you could do:
- Right-click the sheet tab.
- Select 'View Code' from the context menu.
- Copy the code listed below into the worksheet module.
- Change A2,C2,A4,C4 to the list of cells that you want to protect. You can use ranges too: A2:B10,D2:H10
- Switch back to Excel.
- Save the workbook as a macro-enabled workbook (*.xlsm).
- Instruct users to allow macros when they open the workbook.
Private Sub Worksheet_Change(ByVal Target As Range)
If Not Intersect(Range("A2,C2,A4,C4"), Target) Is Nothing Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
End If
End Sub
carolineharten
May 05, 2022Copper Contributor
HansVogelaar This worked for me! I am building a financial calculator for my job and this did the trick, thanks!
- HansVogelaarMay 05, 2022MVP
Nice to hear that!