Consulting

Results 1 to 1 of 1

Thread: CellDragAndDrop disabled in all worksheets

  1. #1
    VBAX Newbie
    Joined
    Jun 2017
    Posts
    1
    Location

    CellDragAndDrop disabled in all worksheets

    Hi

    I'm using the below code (from Ken Puls) to restrict users from using copy/paste in a specifik worksheet.

    Unfortunately it disables CellDragAndDrop on all worksheets - also after the specifik worksheet has been closed (and even after restart of PC)

    I'm new to VBA and can't for the life of me figure out what's wrong???

    Jakob

    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
    Last edited by Paul_Hossler; 06-22-2017 at 06:33 AM. Reason: Added CODE tags, please use the [#] to add them

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •