简体   繁体   English

禁用/启用复制粘贴

[英]Disable/Enable Copy Paste

I have these two codes:我有这两个代码:

The first is a macro to disable copy-paste:第一个是禁用复制粘贴的宏:

Sub Desable_Copy()

    Dim oCtrl As Office.CommandBarControl

    For Each oCtrl In Application.CommandBars.FindControls(ID:=21)
        oCtrl.Enabled = False
    Next oCtrl

    For Each oCtrl In Application.CommandBars.FindControls(ID:=19)
        oCtrl.Enabled = False
    Next oCtrl

    Application.CellDragAndDrop = False
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    With Application
        .CellDragAndDrop = False
        .CutCopyMode = False 'Clear clipboard
    End With
End Sub

The second is a macro to enable copy-paste:第二个是启用复制粘贴的宏:

Sub Enable_Copy()

    Dim oCtrl As Office.CommandBarControl

    For Each oCtrl In Application.CommandBars.FindControls(ID:=21)
        oCtrl.Enabled = True
    Next oCtrl

    For Each oCtrl In Application.CommandBars.FindControls(ID:=19)
        oCtrl.Enabled = True
    Next oCtrl

    Application.CellDragAndDrop = True
End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    With Application
        .CellDragAndDrop = True
        .CutCopyMode = True 'Clear clipboard
    End With
End Sub

When I execute the code I have an error message:当我执行代码时,我收到一条错误消息:

Ambiguous Name Detected检测到不明确的名称

Excel's Copy/Paste capabilities are set for the Excel application. Excel 的复制/粘贴功能是为 Excel 应用程序设置的。 If you disable them for one workbook they are disabled for all.如果您为一个工作簿禁用它们,则它们将被所有工作簿禁用。 If you have several workbooks open at the same time the management becomes quite a chore - if you are an expert programmer which perhaps you are not.如果您同时打开多个工作簿,管理将变得非常繁琐——如果您是一名专家程序员,而您可能不是。 Consider alternatives, such as Application.Undo which can be made to run on the Worksheet_Change event.考虑替代方案,例如可以在 Worksheet_Change 事件上运行的Application.Undo The following code will undo any Paste operation on the worksheet.以下代码将撤消工作表上的任何粘贴操作。

Private Sub Worksheet_Change(ByVal Target As Range)
    ' 18 Apr 2017

    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

This code was adapted from code published here (edit ML: original link is broken, I found this one as a substitute /edit).此代码改编自此处发布的代码(编辑 ML:原始链接已损坏,我找到了作为替代品/编辑)。 The view taken there is not to prevent paste operations but to prevent paste operations from messing up sheet formatting.那里采取的观点不是为了防止粘贴操作,而是为了防止粘贴操作弄乱工作表格式。 It is a very interesting piece, well explained and easy to implement.这是一个非常有趣的作品,解释得很好,很容易实现。

You got 2 private Subs with the same name.您有 2 个同名的私人订阅者。

For example, you can change the second one:例如,您可以更改第二个:

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)

to

Private Sub Workbook_SheetSelectionChangeEnable(ByVal Sh As Object, ByVal Target As Range)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM