Prevent deletion of Excel tables.

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,832
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Assume I have the following formatted as an Excel table:

1618069801087.png



I would to prevent users from deleting the table (essentially prevent row 1 from being deleted).

This is my code but it doesn't work:

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

   If Target.Row = 1 Then
    
        Application.EnableEvents = False
        
        Application.Undo
        
        Application.EnableEvents = True
        
    End If
    
End Sub

If the user selects row 1 and presses the delete key (ie clear contents), I see this:

1618070037845.png


How can I amend my code to prevent users deleting row 1?

Thanks
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Try this:
Let's say the header is in A1:C1

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("A1:C1")) Is Nothing Then
        Application.EnableEvents = False
        On Error GoTo Skip:
        Application.Undo
        Application.OnUndo "", ""
        
    End If

Skip:
Application.EnableEvents = True
End Sub
 
Upvote 0
Try this:
Let's say the header is in A1:C1

VBA Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Not Intersect(Target, Range("A1:C1")) Is Nothing Then
        Application.EnableEvents = False
        On Error GoTo Skip:
        Application.Undo
        Application.OnUndo "", ""
       
    End If

Skip:
Application.EnableEvents = True
End Sub

Thanks for the solution, it worked as expected.

However I do have one query relating to your code.

Put a break point on this line:

Code:
If Not Intersect(Target, Range("A1:C1")) Is Nothing Then

then try to delete / clearcontents anything in row 1.

The code will hit the break point, as expected.

Step into the code all the way to End Sub.

Then if you press F8 (to step into it AFTER End Sub), the code jumps to this line:

Code:
If Not Intersect(Target, Range("A1:C1")) Is Nothing Then

now if you step into it all the way to End Sub, it ends, as expected.

My question is: why does it continue after End Sub the first time?
 
Upvote 0
Actually I don't quite understand this behavior. It looks like if you delete a header it will trigger the Worksheet_Change more than once even if you have set Application.EnableEvents = False. So in your code it will run Application.Undo multiple times which generates wrong result.

What my code does is clear the undo stack with Application.OnUndo "", "" after Application.Undo is executed the first time, so it is only executed once, which gives the correct result.

Maybe some experts here can give a better explanation.
 
Upvote 0
Actually I don't quite understand this behavior. It looks like if you delete a header it will trigger the Worksheet_Change more than once even if you have set Application.EnableEvents = False. So in your code it will run Application.Undo multiple times which generates wrong result.

What my code does is clear the undo stack with Application.OnUndo "", "" after Application.Undo is executed the first time, so it is only executed once, which gives the correct result.

Maybe some experts here can give a better explanation.
Thanks for your reply.

I've had similar (unusual) experiences with Events in the past.

As you said, hopefully someone else might be able to explain this odd behaviour.
 
Upvote 0

Forum statistics

Threads
1,214,943
Messages
6,122,369
Members
449,080
Latest member
Armadillos

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