Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过VBA复制公式时防止Excel单元格受到保护_Vba_Excel_Excel 2016 - Fatal编程技术网

通过VBA复制公式时防止Excel单元格受到保护

通过VBA复制公式时防止Excel单元格受到保护,vba,excel,excel-2016,Vba,Excel,Excel 2016,我使用VBA代码在Excel 2016工作表中插入新行,然后将公式从上行复制到新行,这样我就不必手动插入它们。不幸的是,插入公式的单元格在复制公式后受到保护。这带来了一个缺点,即如果我犯了错误,就无法删除新行。这是我的密码 Private Sub Worksheet_Change(ByVal Target As Range) ' Disable events to prevent that insertion of the row triggers a new Worksheet_Change

我使用VBA代码在Excel 2016工作表中插入新行,然后将公式从上行复制到新行,这样我就不必手动插入它们。不幸的是,插入公式的单元格在复制公式后受到保护。这带来了一个缺点,即如果我犯了错误,就无法删除新行。这是我的密码

Private Sub Worksheet_Change(ByVal Target As Range)

' Disable events to prevent that insertion of the row triggers a new Worksheet_Change
' event. Without this action the worksheet would be in an endles loop.
Application.EnableEvents = False

If Target.Column = 1 Then
    If Cells(Target.Row + 2, 1).Value = "end" Then
        Cells(Target.Row + 1, 1).EntireRow.Insert
    End If
End If

' Copy formulas
Cells(Target.Row - 1, 2).Copy (Cells(Target.Row, 2))
Cells(Target.Row - 1, 5).Copy (Cells(Target.Row, 5))
Cells(Target.Row - 1, 2).Unprotect
Cells(Target.Row - 2, 5).Unprotect


' Reactivate events so that the worksheet can react to user changes.
Application.EnableEvents = True

End Sub
正如你所看到的,我也试图解除对手机的保护,但这没有帮助。复制公式后,如何防止这些单元格受到保护?我已经试了几个小时了,谷歌直到现在才回答我的问题

谢谢你的帮助

看一看。看起来您需要先取消对整个工作表的保护,然后设置要解锁的单元格,然后再次对工作表进行保护。请查看。看起来您需要首先取消对整个工作表的保护,然后设置要解锁的单元格,然后再次对工作表进行保护。