Undo delete on locked cells (VBA)

Craig__

Board Regular
Joined
Feb 16, 2010
Messages
66
I have a worksheet I want to keep unprotected but still prevent data in locked cells from being deleted. I tried using a Worksheet_Change event but can’t get it to work. Your help would be much appreciated.

When a range of cells is selected (cells could be a mixture of locked and unlocked), and the Delete key is pressed, I want the macro to delete only the data in unlocked cells.

Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Selection, Columns("A:G")) Is Nothing Then

Dim rng As Range, cell As Range
Set rng = Selection

For Each cell In rng
If cell.Locked = True Then
Application.EnableEvents = False
Application.Undo
Application.EnableEvents = True
End If
Next cell

End If
End Sub
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
I have experimented a bit more and found that the code below sort of works but not totally reliable. Seems to work on some columns but not others. Not sure why. Would be grateful if someone might be able to develop it a bit further.


Private Sub Worksheet_Change(ByVal Target As Range)

If Not Intersect(Selection, Columns("A:G")) Is Nothing Then

Application.OnKey "{DELETE}", "Locked_Cells_Check"
End If

End Sub

Sub Locked_Cells_Check()
Dim rng As Range, cell As Range
Set rng = Selection

For Each cell In rng
If cell.Locked = False Then
cell.ClearContents
End If
Next cell

Application.OnKey "{DELETE}" 'Return Del key to normal state
End Sub
 
Upvote 0
The Worksheet_Change event fires after the user presses the Delete key. So it may be too late to set up the OnKey.
 
Upvote 0
Many thanks for your advice on this Andrew. So looks like the OnKey approach is also a no no. Back to the drawing board.
 
Upvote 0
Finally, I got it work with this:

Private Sub Worksheet_Activate()
Application.OnKey "{DELETE}", "Locked_Cells_Check"
End Sub

Private Sub Worksheet_Deactivate()
Application.OnKey "{DELETE}"
End Sub

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Application.OnKey "{DELETE}"
End Sub

Sub Locked_Cells_Check()
Dim rng As Range, cell As Range
Set rng = Selection

For Each cell In rng
If cell.Locked = False Then
cell.ClearContents
End If
Next cell
End Sub
 
Upvote 0
Only the contents of unlocked cells within selection are cleared.
Contents of any locked cells within selection are not cleared.
Which is what I need.
 
Upvote 0
You code clears the locked cells within the selection. But the user has pressed Delete, so aren't all the cells already cleared?
 
Upvote 0

Forum statistics

Threads
1,224,585
Messages
6,179,703
Members
452,938
Latest member
babeneker

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top