我正在使用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

正如你所看到的,我也试图取消保护细胞,但这没有用 . 复制配方后,我该怎么做才能防止这些细胞受到保护?我正在尝试这几个小时,谷歌直到现在才回答我的问题 .

谢谢你的帮助!