简体   繁体   中英

Override the cut/paste shortcut keys in Excel using VBA?

Can the cut/paste shortcut keys (ctrl+x, ctrl+v) be overridden in VBA (or through some other way within Excel) to only cut and paste values and text properties, but not fill or borders? Paste-special doesn't work with cut afaik, but I also have code running after each change that makes manual paste-special unusable (afaik) anyway. For reference - though I doubt it matters - the code I have running is to play chess on a toroidal board with a half-twist. Again, my question isn't about that, but if it would help to see it I can paste it in. It is worksheet-change.

Lapid Palid,

Welcome to StackOverflow!

Updated: Check to see if clipboard has data, if not message and Exit

You can do it by creating a macro in a new module:

'                         +-------------------------+             +----------+
'-------------------------|   Sub PasteasValue()    |-------------| 04/29/21 |
'                         +-------------------------+             +----------+
' Shortcut Key: Ctrl+Shift+V

Sub PasteasValue()

  Dim vCBFmts As Variant

'*** Test to see if there is data on the clipboard ***
   vCBFmts = Application.ClipboardFormats
   If UBound(vCBFmts) < 2 Then
     MsgBox "There is no data on the cliboard!", _
            vbOKOnly + vbCritical, _
            "Error: No Data to Import"
     Exit Sub
   End If

  Selection.PasteSpecial Paste:=xlPasteValues
  Application.CutCopyMode = False

End Sub 'PasteasValue()

Then back in your workbook press Alt+F8, select the macro, Click Options and assign v as the shortcut.

Note: I didn't think this would work if pasting from other than Excel do to the use of Selection. However, I tested copying some red text from Word into Excel and it still worked.

HTH

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM