Excel Tables

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,834
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
My worksheet contains a headings row (cells A1 to C1) and 1 row of data (cells A2 to C2).

I want to prevent users from altering the headings, so I add this code:

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 tries to alter any value in cells A1 to C1, the code reverses it.

If the user tries to type in any cell in row 1, eg D1 or E1, the code also reverse it, which is what I want.

However, if the range (A1 to C2) were converted into a table (by pressing Ctrl+T), things don't quite go according to plan.

If the user tries to alter any value in cells A1 to C1, the code reverses it, (as expected).

However, if the user tries to type in any cell in row 1, eg D1 or E1, Excel extends the table range to include this new cell address, then the code reverses this last action (the extending of the table).

Whatever value was entered still remains.

How can I change it so the value typed is reversed as well?

Thanks
 

Excel Facts

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off
Could you instead:
Select row A > Format Cells > Protection > Locked
Protect Sheet (apply a password)
Protect Workbook (apply a password)
 
Upvote 0
Sorry, just realized my previous post was a bit misleading...
Excel defaults all cells to Protected status, therefore would have to:

Select the entire tab (click the triangle in top left corner of tab)
Set all cells to Unlocked
Then select row 1 > Format Cells > Protection > Locked
Protect Sheet (apply a password)
Protect Workbook (apply a password)
 
Upvote 0
Sorry, just realized my previous post was a bit misleading...
Excel defaults all cells to Protected status, therefore would have to:

Select the entire tab (click the triangle in top left corner of tab)
Set all cells to Unlocked
Then select row 1 > Format Cells > Protection > Locked
Protect Sheet (apply a password)
Protect Workbook (apply a password)
Thanks for your suggestions but I'd rather not resort to passwords and protecting the sheet.

Instead, I have decided to delete the columns to solve this particular problem.

Code:
Private Sub Worksheet_Change(ByVal Target As Range)

    If Target.Column > 3 Then
    
        With Application
        
            .ScreenUpdating = False
            .EnableEvents = False
        
        End With
        
        Me.Columns((Target.Column)).Delete

        With Application
        
            .ScreenUpdating = True
            .EnableEvents = True
    
        End With
    
    End If

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,214,976
Messages
6,122,541
Members
449,089
Latest member
davidcom

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