禁用复制/粘贴

时间:2015-11-02 07:36:03

标签: excel excel-vba vba

Excel中禁用的复制粘贴正在使用以下代码:

Option Explicit
Sub ToggleCutCopyAndPaste(Allow As Boolean)
    Call EnableMenuItem(21, Allow)
    Call EnableMenuItem(19, Allow)
    Call EnableMenuItem(22, Allow)
    Call EnableMenuItem(755, Allow)
    Application.CellDragAndDrop = Allow
    With Application
        Select Case Allow
            Case Is = False
                .OnKey "^c", "CutCopyPasteDisabled"
                .OnKey "^v", "CutCopyPasteDisabled"
                .OnKey "^x", "CutCopyPasteDisabled"
                .OnKey "+{DEL}", "CutCopyPasteDisabled"
                .OnKey "^{INSERT}", "CutCopyPasteDisabled"
            Case Is = True
                .OnKey "^c"
                .OnKey "^v"
                .OnKey "^x"
                .OnKey "+{DEL}"
                .OnKey "^{INSERT}"
            End Select
    End With
End Sub
Sub EnableMenuItem(ctlId As Integer, Enabled As Boolean)
    Dim cBar As CommandBar
    Dim cBarCtrl As CommandBarControl
    For Each cBar In Application.CommandBars
        If cBar.Name <> "Clipboard" Then
            Set cBarCtrl = cBar.FindControl(ID:=ctlId, recursive:=True)
            If Not cBarCtrl Is Nothing Then cBarCtrl.Enabled = Enabled
        End If
    Next
End Sub
Sub CutCopyPasteDisabled()
    MsgBox "Sorry!  Cutting, copying and pasting have been disabled in this workbook!"
End Sub

但是当我双击一个单元格然后我可以做任何切割,复制和粘贴。

如何禁用复制粘贴,甚至双击进入单元格?

2 个答案:

答案 0 :(得分:0)

您可以使用Worksheet.Protect保护工作表,例如:

Sheets("Sheet1").Protect "Password"

现在用户无法选择或更改单元格中的值。 您也可以锁定单元格manually

您还可以Unprotect工作表:

Sheets("Sheet1").Unprotect "Password"

这是您在更改工作表之前需要做的(也是通过VBA)。

编辑1 :我不知道如何让用户仍然编辑单元格,但不能复制粘贴它们的值。对此的另一个“解决方案”可以是让用户通过表单输入数据。作为一个例子,我在工作表上添加了一个按钮:

add excel button

(注意:如果您看不到“开发者”标签you will have to add it。)现在,系统会询问您分配哪个宏,选择新建。现在,您可以使用以下代码填充分配给按钮的子(宏):

Sub Button1_Click()
Sheets("Sheet1").Unprotect "Password"
Sheets("Sheet1").Range("A1") = InputBox("New value")
Sheets("Sheet1").Protect "Password"
End Sub

现在用户只能更改该单元格的值,在本例中为A1。

答案 1 :(得分:0)

这可能有帮助吗?

在Thisworkbook(Code)页面上输入

Private Sub Workbook_SheetBeforeDoubleClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Cancel = True
    MsgBox "Double click disabled!"
End Sub

https://i.stack.imgur.com/WSraj.jpg