Forum Discussion
How can i protect a calculated column in a table from being edited but allowing users to add rows?
You do need VBA for this, sorry!
- TonyCrabbMay 04, 2022Copper ContributorThanks HansVogelaar - you have been very helpful. I need to sleep on this one and have a think about where to go next.
- HansVogelaarMay 04, 2022MVP
You can remove the Beep line and change Range("A1").Select to Range("E" & Target.Row).Select
- TonyCrabbMay 04, 2022Copper ContributorThanks HansVogelaar. instead of the beep and going to A1, can you find current row and just move the cursor automatically to Col E on current row?
- mathetesMay 04, 2022Gold Contributor
This may be a silly solution, but I wondered if--unless you're worried that people could intentionally or maliciously override your calculations--you couldn't just move the calculating column off to the right and hide it to prevent inadvertent entries. (Or, to be more extreme, put the calculations on a separate sheet, and hide the sheet).
- HansVogelaarMay 04, 2022MVP
That is too difficult (we don't know how the user arrived there), but you can do something like this if you really want to irritate your users:
- Right-click the sheet tab.
- Select View Code from the context menu.
- Copy the code listed below into the worksheet module.
- Change Range("F:F") to the column you want to make inaccessible.
- If you ever want to edit the column, type the following in the Immediate window of the Visual Basic Editor and press Enter:
Application.EnableEvents = False - When you're done, do the same but with
Application.EnableEvents = True - Don't forget this, otherwise the worksheet won't react to events anymore.
Private Sub Worksheet_SelectionChange(ByVal Target As Range) If Not Intersect(Range("F:F"), Target) Is Nothing Then Application.EnableEvents = False Beep Range("A1").Select Application.EnableEvents = True End If End Sub - TonyCrabbMay 04, 2022Copper ContributorThanks HansVogelaar. This is great and so powerful but we are still having to re-create a massive amount of features just to protect a calculated column (eg i need to solve this for deleting, sorting etc as well).
Coming at this a slightly different way, is it possible to fire a macro when i enter a column so that it automatically jumps over that column? so in my calculated column example, could i leave the sheet unprotected but if i try to enter the calculated columm via a click or a tab or an arrow ( eg some sort of on Select) could it automatically move me to the next or previous column? - HansVogelaarMay 04, 2022MVP
This version will try to insert a new row in the row of the active cell, so you need only one button to call the macro.
Check out the wsh.Protect part and change the True/False arguments as needed.
Sub AddRow() Dim wsh As Worksheet Dim tbl As ListObject Dim pwd As String Dim pos As Long Application.ScreenUpdating = False pwd = "secret" Set wsh = ActiveSheet wsh.Unprotect Password:=pwd Set tbl = ActiveSheet.ListObjects(1) pos = ActiveCell.Row - tbl.Range.Row tbl.ListRows.Add Position:=pos, AlwaysInsert:=False wsh.Protect Password:=pwd, DrawingObjects:=False, _ Contents:=True, Scenarios:=False, _ AllowFormattingCells:=True, AllowFormattingColumns:=True, _ AllowFormattingRows:=True, AllowInsertingColumns:=True, _ AllowInsertingRows:=True, AllowInsertingHyperlinks:=True, _ AllowDeletingColumns:=True, AllowDeletingRows:=True, _ AllowSorting:=True, AllowFiltering:=True, _ AllowUsingPivotTables:=True Application.ScreenUpdating = True End Sub - TonyCrabbMay 04, 2022Copper ContributorThanks HansVogelaar i have set this up and added a button to activate and it does now add a row and then relock which is great! But it raises a few new questions:
1. the row always adds at the bottom of the table. can it add based on the row the cursor is on?
2. in my example, i had enabled things like sort and filter etc on the protect options but the macro resets all of those so it doesnt allow me to filter after inserting
3. is there a way to link the macro to the standard row insert instead of me adding a new button? - HansVogelaarMay 04, 2022MVP
For example:
Sub AddRow() Dim strPassword As String strPassword = "secret" Application.ScreenUpdating = False ActiveSheet.Unprotect Password:=strPassword ActiveSheet.ListObjects(1).ListRows.Add AlwaysInsert:=False ActiveSheet.Protect Password:=strPassword Application.ScreenUpdating = True End Sub - TonyCrabbMay 04, 2022Copper Contributorthanks HansVogelaar. Any advice on how?