Hi,
I’ve been searching for a solution on disabling cut/paste/copy functions in selected cell ranges rather than the entire worksheet. I couldn't find a really suitable solution until I found the very useful bit of code included in this post. For this I am really grateful.
As you will see pasted below, I’ve made some very minor modifications to allow a single column to have all the functions and I blocked a certain cell range.
It works perfectly, except for one small thing. I can’t figure out if I did something wrong to cause it, or if something changed in Excel 2003 (I’m on Sp3) since this original post was made.
The cell range I’ve blocked from using cut/copy/paste successfully blocks cut/copy/paste from right clicks, cut and copy on the toolbar, but the paste button on the toolbar is still visible.
Can anyone please point me in the right direction?
*** In the ThisWorkbook 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 Private Sub Workbook_Activate() 'Force the current selection to be selected, triggering the appropriate 'state of the cut, copy & paste commands Selection.Select End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) 'Re-enable the cut, copy & paste commands Call ToggleCutCopyAndPaste(True) End Sub Private Sub Workbook_Deactivate() 'Re-enable the cut, copy & paste commands Call ToggleCutCopyAndPaste(True) End Sub Private Sub Workbook_Open() 'Force the current selection to be selected, triggering the appropriate 'state of the cut, copy & paste commands Selection.Select End Sub Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range) 'Toggle the cut, copy & paste commands on selected ranges Select Case Sh.Name Case Is = "Contracting Activity" 'Disable cut, copy & paste for Contracting Activity, Range D20:AD325 If Not Intersect(Target, Target.Parent.Range("D20:AD325")) Is Nothing Then Call ToggleCutCopyAndPaste(False) Else Call ToggleCutCopyAndPaste(True) End If Case Is = "Planned Activity" 'Disable cut, copy & paste for Planned Activity, Range D20:AD325 If Not Intersect(Target, Target.Parent.Range("D20:AD325")) Is Nothing Then Call ToggleCutCopyAndPaste(False) Else Call ToggleCutCopyAndPaste(True) End If Case Else 'Re-enable cut copy and paste commands as this is not a restricted sheet Call ToggleCutCopyAndPaste(True) End Select End Sub




Reply With Quote