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

时间:2017-10-29 06:52:55

标签: excel vba excel-vba

我有一些Excel宏,人们在执行之前手动将其粘贴到数据中。

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

我试过

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

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

1 个答案:

答案 0 :(得分:3)

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

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