在Excel中限制粘贴

时间:2016-01-28 18:32:33

标签: excel-vba excel-2010 vba excel

我正在使用受保护的工作簿,该工作簿只允许用户选择并将数据输入某些单元格 但是,他们倾向于将数据粘贴到这些未锁定的单元格中,我想在Excel 2010中限制此行为。我发现了一些较旧的VBA代码,但似乎功能区使其大部分都已过时。

2 个答案:

答案 0 :(得分:1)

您可能需要尝试这样的事情,我将这些子设备放在工作簿模块中

Private Sub Workbook_SheetActivate(ByVal Sh As Object)
    Application.CutCopyMode = False

End Sub

Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
    Application.CutCopyMode = False

End Sub

Private Sub Workbook_SheetBeforeRightClick(ByVal Sh As Object, ByVal Target As Range, Cancel As Boolean)
    Application.CutCopyMode = False

End Sub

Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    Application.CutCopyMode = False

End Sub

答案 1 :(得分:0)

我遇到了以下仍在Excel 2010中运行的代码。

'*** In a standard module ***
Option Explicit 

Sub ToggleCutCopyAndPaste(Allow As Boolean) 
     'Activate/deactivate cut, copy, paste and pastespecial menu items
    Call EnableMenuItem(21, Allow) ' cut
    Call EnableMenuItem(19, Allow) ' copy
    Call EnableMenuItem(22, Allow) ' paste
    Call EnableMenuItem(755, Allow) ' pastespecial

     'Activate/deactivate drag and drop ability
    Application.CellDragAndDrop = Allow 

     'Activate/deactivate cut, copy, paste and pastespecial shortcut keys
    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) 
     'Activate/Deactivate specific menu item
    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() 
     'Inform user that the functions have been disabled
    MsgBox "Sorry!  Cutting, copying and pasting have been disabled in this workbook!" 
    End Sub 

 '*** In the ThisWorkbook Module ***
Option Explicit 

Private Sub Workbook_Activate() 
    Call ToggleCutCopyAndPaste(False) 
End Sub 

Private Sub Workbook_BeforeClose(Cancel As Boolean) 
    Call ToggleCutCopyAndPaste(True) 
End Sub 

Private Sub Workbook_Deactivate() 
    Call ToggleCutCopyAndPaste(True) 
End Sub 

Private Sub Workbook_Open() 
    Call ToggleCutCopyAndPaste(False) 
End Sub

信息的链接是:http://www.vbaexpress.com/kb/getarticle.php?kb_id=373