Alan Kidd
Alan Kidd

Reputation: 3

Using Application.OnKey to Disable CTRL-V

I am trying to disable CTRL+V in one of the Workbooks that I have open, but it is being disabled in all open workbooks.

Is there a way of doing this, as I have tried using it in both the Sheet that I want to disable CTRL+V in and the ThisWorkbook, both with the same result, CTRL+V is disabled in all open Workbooks and all Sheets in those Workbooks.

Code I have at the Sheet level:

Sub DisableCtrlPasteCombinations()
' Disable CTRL-V
Application.OnKey "^{v}", ""
' Disable CTRL-ALT-V
Application.OnKey "^%{v}", ""
End Sub

Code I have tried at ThisWorkbook level:

Private Sub Workbook_Open()
' Disable CTRL-V
Application.OnKey "^{v}", ""
' Disable CTRL-ALT-V
Application.OnKey "^%{v}", ""
End Sub

Upvotes: 0

Views: 1383

Answers (1)

Glitch_Doctor
Glitch_Doctor

Reputation: 3034

Only thing I can think of would be to perform a check on ActiveWorkbook.Name first:

Sub DisableCtrlPasteCombinations()
    ' Disable CTRL-V
    Application.OnKey "^{v}", "PasteController"
End Sub

Sub PasteController()
    IF ActiveWorkbook.Name <> "WorkbookName" THEN
        Selection.Paste
    END IF
    ' Otherwise do nothing
End Sub

Upvotes: 3

Related Questions