Excel VBA禁用从工作表中粘贴特定范围

我有一些Excelmacros,人们在执行之前手动粘贴到它的数据。

为了避免macros运行中的错误,我想禁用粘贴某些列的数据。

我试着用

Private Sub Worksheet_Change(ByVal Target As Range) Target.Column = 7 Target.Column = 8 Target.Column = 12 End Sub 

有人能指导我怎么可能吗?

尝试下面的代码,将其添加到您要禁用用户粘贴到某些列的工作表中。

 Private Sub Worksheet_Change(ByVal Target As Range) ' Restrict the user from deleting or Pasting to certain columns Select Case Target.Column Case 7, 8, 12 With Application .EnableEvents = False .Undo MsgBox "Pasting to columns 'F' , 'G' or 'L' is not allowed", vbCritical .EnableEvents = True End With Case Else ' do nothing / or something else End Select End Sub