Option Explicit
Private Sub Worksheet_Change(ByVal Target As Range)
Dim vPrev As Variant
Dim vNew As Variant
Dim lRow As Long
Dim lCol As Long
Dim bMsgGiven As Boolean
Application.EnableEvents = False
vNew = Target.Formula
Application.Undo
vPrev = Target.Formula
Application.EnableEvents = True
If IsArray(vPrev) Then
For lRow = LBound(vPrev, 1) To UBound(vPrev, 1)
For lCol = LBound(vPrev, 2) To UBound(vPrev, 2)
If Len(vNew(lRow, lCol)) = 0 And Len(vPrev(lRow, lCol)) > 0 Then
If Not bMsgGiven Then
'only display this message once
MsgBox "Deleting content on this worksheet is not allowed!", vbExclamation
bMsgGiven = True
End If
'restore previous formula
vNew(lRow, lCol) = vPrev(lRow, lCol)
End If
Next
Next
'Restore all formulas now
'prevent this routine to cause a call to itself
Application.EnableEvents = False
Target.Formula = vNew
Application.EnableEvents = True
Else
If Len(vNew) = 0 And Len(vPrev) > 0 Then
MsgBox "Deleting content on this worksheet is not allowed!", vbExclamation
'Something was deleted, restore all formulas now
'prevent this routine to cause a call to itself
Application.EnableEvents = False
Target.Formula = vPrev
Application.EnableEvents = True
Else
Application.EnableEvents = False
Target.Formula = vNew
Application.EnableEvents = True
End If
End If
End Sub