Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VBA event trigger on copy?

Tags:

excel

vba

Is there a method in VBA, where when in excel I copy a cell's value it triggers my function?

Or any work-arounds for that?

Or can I read key-downs and listen to Ctrl+C ?

Or can VBA read the content of copied text?

The reason is: I copy some entries from an Excel sheet to another program, and i would like to add automatically for example a gray font color to all entries which are already copied.

like image 292
user2520818 Avatar asked Mar 22 '26 15:03

user2520818


1 Answers

Assign a shortcut key to your macro.

For example, if your macro is this:

Sub CopyAndMarkAsCopied()
    Dim r As Range
    Set r = Selection
    With r
        .Copy
        .Font.Color = RGB(100, 100, 100) 'dark grey font
        .Interior.Color = RGB(200, 200, 200) 'light gray background
        'whatever else
    End With
End Sub

In Excel 2010, view macros in Developer > Macros (or keyboard shortcut Alt-F8). In earlier versions of Excel, the menus are a bit different, but the keyboard shortcut works the same.

Select your macro in the list, and click Options.... In this dialog, you can assign a shortcut key to your macro. It can either be:

  • Ctrl-some letter, in which case you have to type a lowercase letter; or
  • Shift-Ctrl-some letter; type an uppercase letter.

In this example I chose Ctrl-Shift-C (note the uppercase C in the screenshot below).

I probably wouldn't mask default keyboard shortcuts that I normally use, like Ctrl-C, but you're free to do so if that's your thing.

enter image description here

Example in action when pressing Ctrl-Shift-C:

like image 81
Jean-François Corbett Avatar answered Mar 24 '26 12:03

Jean-François Corbett



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!