Disable Cntrl_C and Cntrl_V in VBA

C

Celtic_Avenger

I have been very kindly given the code to disable the right mouse butto
in a spreadsheet I am creating.

I have found that if I create a new macro in this workbook and assig
Ctrl_C to it, but then leave the macro blank, this does what I want
but If I open another workbook at the same time, Ctrl_C remain
disabled until I close the workbook that contains the blank macro.

I would like to know if there is a way to disable the Ctrl_C and Ctrl_
shortcuts in this workbook only, but allow them to work in other ope
workbooks.

Is this possible?

thanks

Celtic_Avenger
:confused: :cool: :confused: :cool: :confused
 
S

Stephen Bullen

Hi Celtic_Avenger,
I would like to know if there is a way to disable the Ctrl_C and Ctrl_V
shortcuts in this workbook only, but allow them to work in other open
workbooks.

Sure. You need to hook the workbook's events and disable them when one
of the workbook's windows is activated and enable them when one of the
workbook's windows is deactivated. I guess you also need to handle
Shift+Insert and Ctrl+Insert as well. Put the following in the
ThisWorkbook module:

Private Sub Workbook_WindowActivate(ByVal Wn As Window)
'Disable when switching to us
Application.OnKey "^C", ""
Application.OnKey "^c", ""
Application.OnKey "^{INSERT}", ""

Application.OnKey "^V", ""
Application.OnKey "^v", ""
Application.OnKey "+{INSERT}", ""
End Sub

Private Sub Workbook_WindowDeactivate(ByVal Wn As Window)
'Enable when switching away
Application.OnKey "^C"
Application.OnKey "^c"
Application.OnKey "^{INSERT}"

Application.OnKey "^V"
Application.OnKey "^v"
Application.OnKey "+{INSERT}"
End Sub


Regards

Stephen Bullen
Microsoft MVP - Excel
www.BMSLtd.ie
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top