Excel VBA-禁用工作簿中的复制/剪切粘贴

时间:2018-07-23 08:39:48

标签: excel vba excel-vba

我正在尝试寻找一种方法来防止在Excel单元格中进行复制/剪切粘贴。我禁用了单元格的复制/粘贴功能,但是发现了一个缺陷,即如果用户复制了该单元格的实际值,他就可以粘贴(仅在1个单元格而不是单元格范围内),从而使禁用的复制/粘贴功能无效。

我从另一个问题中使用了以下代码行:

Private Sub Worksheet_Change(ByVal Target As Range)
Application.CutCopyMode = False

Dim UndoList As String

With Application
    .EnableEvents = False
    .ScreenUpdating = False
End With

On Error GoTo ErrExit
UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(UndoList, 5) = "Paste" Or UndoList = "Auto Fill" Then
    MsgBox "Please don't paste values on this sheet." & vbCr & _
           "The action will be reversed.", vbInformation, _
           "Paste is not permitted"
    With Application
        .Undo
        .CutCopyMode = False
    End With
    Target.Select
End If

ErrExit:
With Application
    .ScreenUpdating = True
    .EnableEvents = True
End With
End Sub

1 个答案:

答案 0 :(得分:1)

如何使用Application.Onkey

Sub DisableCutCopyPaste()
Application.OnKey "^{c}", "" 'Copy
Application.OnKey "^{v}", "" 'Paste
Application.OnKey "^{x}", "" 'Cut
End Sub