Excel 在允许新行/删除行的同时保护表中的公式

Excel 在允许新行/删除行的同时保护表中的公式,excel,vba,Excel,Vba,我使用的一些表是为方便用户输入数据而设计的。有些列是公式。我想保护使用公式的列,但仍然允许用户添加新行并从表中删除现有行 这里有一些非常有用的代码: Private子工作表\u更改(ByVal目标作为范围) 作为ListObject的Dim lstObj 可调暗范围 Dim rngOffsetTbl As范围 如果Target.Cells.Count>1,则如果更改了多个单元格(如复制粘贴),则退出Sub' 如果Target.Value=”“,则如果值已删除,则退出子项 返回可重入事件时出错 A

我使用的一些表是为方便用户输入数据而设计的。有些列是公式。我想保护使用公式的列,但仍然允许用户添加新行并从表中删除现有行

这里有一些非常有用的代码:

Private子工作表\u更改(ByVal目标作为范围)
作为ListObject的Dim lstObj
可调暗范围
Dim rngOffsetTbl As范围
如果Target.Cells.Count>1,则如果更改了多个单元格(如复制粘贴),则退出Sub'
如果Target.Value=”“,则如果值已删除,则退出子项
返回可重入事件时出错
Application.EnableEvents=False
'将表分配给ListObject变量
'Me是此模块所属工作表的通用对象
设置lstObj=Me.ListObjects(“Table2”)'将“Table2”编辑为表名
Set rngTable=lstObj.Range'将表格范围指定给范围变量
带灯塔
'将表下的下一行指定给范围变量
Set rngOffsetTbl=.Rows(.Rows.Count+1)
以
'测试更改的单元格是否位于表后的下一行
“不是什么都没有,所以目标在表下的下一行
如果不相交(目标,rngOffsetTbl)则为空
'Me是此模块所属工作表的通用对象
Me.Unprotect密码:=strPass
带灯塔
'将rngTable调整为表所需的新大小(例如行数+1)
设置rngTable=.Resize(.Rows.Count+1、.Columns.Count)
以
lstObj.Resize rngTable“将表格调整为新的范围大小
Me.Protect密码:=strPass
如果结束
可重入事件:
如果错误号为0,则
MsgBox“模块中发生错误”&Me.Name&“私有子工作表\u更改”
如果结束
Application.EnableEvents=True
端接头
这允许用户添加新行(很棒),但不允许用户删除现有行。有没有办法做到这一点

第二个问题:在上述代码末尾应用的保护是否继承了工作表上以前使用的保护设置?如果没有,是否有办法做到这一点,或者,如果没有,是否有人可以向我指出使用VBA应用特定保护设置的正确方向?

1。问题 如果列被锁定且工作表受到保护,则在不首先删除保护的情况下,无法删除整行

因此,解决方法是添加一个“删除选定行”按钮(或一个过程的键盘快捷键),然后编写一个代码来取消工作表的保护,删除该行并再次保护它

取消保护/保护的替代方案
研究如何结合
工作簿_Open
事件正确使用
userinterface
参数,这只允许保护用户界面,但VBA仍然可以无限制地访问工作表。这样,您仍然需要一个专用按钮(或快捷方式)来删除行,但不需要每次都取消保护

2.问题 您只需要阅读的官方文件。在这里,您可以看到每个参数(除了
密码
参数)都有一个默认值
True
False
。这就是在不指定参数的情况下使用的方法。它不使用任何事先设置的内容

Private Sub Worksheet_Change(ByVal Target As Range)
   
    Dim lstObj As ListObject
    Dim rngTable As Range
    Dim rngOffsetTbl As Range
   
    If Target.Cells.Count > 1 Then Exit Sub 'If more cells than one changed (as in copy paste)
   
    If Target.Value = "" Then Exit Sub  'If value deleted
   
    On Error GoTo ReEnableEvents
    Application.EnableEvents = False
   
    'Assign the table to a ListObject variable
    'Me is the generic object for the worksheet to which this module belongs
    Set lstObj = Me.ListObjects("Table2")   'Edit "Table2" to your table name
   
    Set rngTable = lstObj.Range 'Assign the table range to a range variable
   
    With rngTable
        'Assign the next row under the table to a range variable
        Set rngOffsetTbl = .Rows(.Rows.Count + 1)
    End With
   
    'Test if the changed cell is in the next row after the table
    'Not nothing then is something so target is in the next row under the table
    If Not Intersect(Target, rngOffsetTbl) Is Nothing Then
        'Me is the generic object for the worksheet to which this module belongs
        Me.Unprotect Password:=strPass
       
        With rngTable
            'Resize rngTable to the new size required for the table (eg. Rows count + 1)
            Set rngTable = .Resize(.Rows.Count + 1, .Columns.Count)
        End With

        lstObj.Resize rngTable  'Resize the table to the new range size
           
        Me.Protect Password:=strPass
    End If
   
ReEnableEvents:
   
        If Err.Number <> 0 Then
            MsgBox "An error occurred in module " & Me.Name & " Private Sub Worksheet_Change"
        End If
       
        Application.EnableEvents = True

End Sub