Disable cut, copy and paste in Excel from Excel or any other Non-Excel Applications

Occasional Contributor

I have seen various articles desribing ways to block or disable cut, copy and paste in Protected Excel Workbook for unlocked cells and I can say I have tried almost everything. The most appealing I found out of the others was the code mentioned in this link: https://www.extendoffice.com/documents/excel/4289-excel-disable-cut-copy-paste.html

 

So this code works as it appeals as it disables cut, copy and paste functionalities from Excel to Excel.
Luckily, I happened to test the same from Notepad or Non-Excel aplication to Excel. It is still letting copy data from Non-Excel Application like Notepad or Chrome to Excel.
I am not an expert in VBA, so if there is anything which can be added to block other pasting options it would great.

 

Code from the link for reference:

 

Private Sub Workbook_Activate()
Application.CutCopyMode = False
Application.OnKey "^c", ""
Application.CellDragAndDrop = False
End Sub
 
Private Sub Workbook_Deactivate()
Application.CellDragAndDrop = True
Application.OnKey "^c"
Application.CutCopyMode = False
End Sub
 
Private Sub Workbook_WindowActivate(ByVal Wn As Window)
Application.CutCopyMode = False
Application.OnKey "^c", ""
Application.CellDragAndDrop = False
End Sub
 
Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
Application.CellDragAndDrop = True
Application.OnKey "^c"
Application.CutCopyMode = False
End Sub
 
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
Application.CutCopyMode = False
End Sub
 
Private Sub Workbook_SheetActivate(ByVal Sh As Object)
Application.OnKey "^c", ""
Application.CellDragAndDrop = False
Application.CutCopyMode = False
End Sub
 
Private Sub Workbook_SheetDeactivate(ByVal Sh As Object)
Application.CutCopyMode = False
End Sub
2 Replies

@Abdullah_Shurjeel 

 

You could also try this: add a worksheet change event handler (or a workbook_sheetchange event handler) to check if the last action in the undo list is "Paste" and, if so, undo.

 

Private Sub Worksheet_Change(ByVal Target As Range)
     Const procName As String = "Worksheet_Change"
     Dim lastAction As String
     
     
     '//  Get the last action performed by user.
     On Error Resume Next
     lastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)
     On Error GoTo ErrHandler
     
     '//  Check if the last action was paste.
     If Left(lastAction, 5) = "Paste" Then
          With Application
               .EnableEvents = False
               .Undo
          End With
     End If


ExitProc:
     Application.EnableEvents = True
     Exit Sub
     
ErrHandler:
     MsgBox prompt:="Error " & Err.Number & ": " & Err.Description, Title:=Me.Name & "." & procName
     Resume ExitProc
     
End Sub
Thanks for the response.

I tried the code but it is still letting paste from non-excel application to my working excel when I right click and paste with source formatting that is even removing formatting.

Note: Control + V is getting disable but not right click.