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
ajitjosh
Jan 06, 2025Copper Contributor
Dear Christina,
This works perfectly, But how to handle this when you need to copy the same set of cells to some other location say 5 lines below the present lines? Please advise.
Thanks
- HansVogelaarJan 06, 2025MVP
This discussion is about locking cells against being edited.
You appear to want something else, so please start a new discussion.