[vba]Option Explicit Sub ToggleCutCopyAndPaste(Allow As Boolean)
'Activate/deactivate cut, copy, paste and pastespecial menu items
Application.CellDragAndDrop = Allow
Call EnableMenuItem(21, Allow)
' cut Call EnableMenuItem(19, Allow)
' copyCall EnableMenuItem(22, Allow)
' paste Call EnableMenuItem(755, Allow)
' pastespecial 'Activate/deactivate drag and drop ability
'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 [/vba]
[vba]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 [/vba]
[vba] Sub CutCopyPasteDisabled() 'Inform user that the functions have been disabled
MsgBox "Sorry! Cutting, copying and pasting have been disabled in this workbook!"
End Sub [/vba]
'*** In the ThisWorkbook Module ***
[vba]Option Explicit Private Sub Workbook_Activate()
Call ToggleCutCopyAndPaste(False)
End Sub [/vba]
[vba]Private Sub Workbook_BeforeClose(Cancel As Boolean)
Call ToggleCutCopyAndPaste(True)
End Sub[/vba]
[vba]Private Sub Workbook_Deactivate()
Call ToggleCutCopyAndPaste(True)
End Sub [/vba]
[vba]Private Sub Workbook_Open()
Call ToggleCutCopyAndPaste(False)
End Sub [/vba]
How to use:
- Copy above code for the standard module.
- In Excel press Alt + F11 to enter the VBE.
- Press Ctrl + R to show the Project Explorer.
- Right-click desired file on left (in bold).
- Choose Insert -> Module.
- Paste code into the right pane.
- Copy the code for the ThisWorkbook module.
- In the project explorer, locate the ThisWorkbook object.
- Double click the ThisWorkbook object.
- Paste code into the right pane.
- Press Alt + Q to close the VBE.
- Save workbook before any other changes.
- Close and reopen the workbook.
Test the code:
- Try any combination of cutting, copying or pasting in whatever means you prefer.