Data Validation - disable delete key in more than one range

milt2010

Board Regular
Joined
Dec 18, 2010
Messages
118
Hi,
I need a code to disable the deletion in different ranges of the same unprotect worksheet. Eg.: in range A1: H13; in range B18:H18; in range B24:H24 and so on.

I found this code that is partially good for my needs:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    If Application.Intersect(Target, Range("C1:F150")) Is Nothing Then Exit Sub
    For Each Cell In Target.Cells
        If Len(Cell.Value) = 0 Then
            Application.EnableEvents = False
            Application.Undo
            MsgBox "Please don't clear cells in this range."
            Application.EnableEvents = True
            Exit For
        End If
    Next Cell
End Sub

But it work only in a range! :(
What I should change in this code to do this?

Thanks
 

Excel Facts

Save Often
If you start asking yourself if now is a good time to save your Excel workbook, the answer is Yes
Ok, I found a way:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim Cell As Range
    If Application.Intersect(Target, Range("A2:H13,B18:H18,B24:H24,B30:H30")) Is Nothing Then Exit Sub
    For Each Cell In Target.Cells
        If Len(Cell.Value) = 0 Then
            Application.EnableEvents = False
            Application.Undo
            MsgBox "Please don't clear cells in this range."
            Application.EnableEvents = True
            Exit For
        End If
    Next Cell
End Sub
This works for my needs.
 
Upvote 0

Forum statistics

Threads
1,214,990
Messages
6,122,625
Members
449,093
Latest member
catterz66

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