Macro : Prevent Delete in a Cell or Multiple Cells

muhammad susanto

Well-known Member
Joined
Jan 8, 2013
Messages
2,102
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
hi all...

i have macro code to prevent delete in a cell..
this code :
Code:
Private Sub Worksheet_Change(ByVal Target As Range)    On Error GoTo Whoa


    Application.EnableEvents = False


    If Not Intersect(Target, Range("A2:A5,B2:B5")) Is Nothing Then
        If Len(Trim(Target.Value)) = 0 Then Application.Undo
    End If


LetsContinue:
    Application.EnableEvents = True
    Exit Sub
Whoa:
    MsgBox Err.Description
    Resume LetsContinue
End Sub

that's code is working but if i select in multiple cell e.g. drag / block from A2 to A5 then Press Del , the macro can't working, show "Type mismatch" then cell is deleted...

how to still keep or prevent the data can't delete when i delete for each cell or multiple cell...

thanks in advance...

m.susanto
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Try:
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    Dim rng As Range
    If Intersect(Target, Range("A2:A5,B2:B5")) Is Nothing Then Exit Sub
    For Each rng In Target
        If Len(Trim(rng.Value)) = 0 Then Application.Undo
    Next rng
End Sub
 
Upvote 0
hi mumps..you're always coming, worked it!!!
thank you...
Are you sure?

Code:
Private Sub Worksheet_Change(ByVal Target As Range)
Dim rng As Range, cel As Range
Set rng = Intersect(Target, Range("A2:A5,B2:B5"))
If Not rng Is Nothing Then
    For Each cel In rng
        If Len(Trim(cel)) = 0 Then
            With Application
                .EnableEvents = False
                .Undo
                .EnableEvents = True
            End With
            Exit Sub
        End If
    Next
End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,824
Messages
6,181,186
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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