java防止vba注入,通过VBA复制公式时,防止Excel单元格受到保护

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

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

谢谢你的帮助!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值