Calling VBA event on copy?

Is there a method in VBA where when in excel I copy a cell value it calls my function?

Or are there any workarounds for this?

Or can I read the keys down and listen to Ctrl + C?

Or can VBA read the content of the copied text?

The reason is that I am copying some records from an Excel sheet to another program and I would like to add automatically, for example, a gray font color to all records that have already been copied.

+3


source to share


3 answers


Assign a shortcut to your macro.

For example, if your macro is like 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 the keyboard shortcut Alt- F8). In earlier versions of Excel, the menus are slightly different, but the keyboard shortcut works the same.

Select your macro from the list and click Options.... In this dialog, you can assign a shortcut to your macro. This could be:

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


In this example, I chose Ctrl- Shift- C(note the upper case C

in the screenshot below).

I probably won't mask the default keyboard shortcuts I usually use, like Ctrl- C, but you can do that if that's your thing.

enter image description here

An example in action when pressing Ctrl- Shift- C:

+3


source


You can assign the Cntrl+ macro cusing the OnKey statement.

Cm:



http://msdn.microsoft.com/en-us/library/office/ff197461(v=office.15).aspx

+1


source


Here is an example of the OnKey clause sentence given earlier:

Application.OnKey "{UP}", "PressUp" Application.OnKey "{DOWN}", "PressDown"

"PressUp" and "PressDown" are the names of the sub-sites to be launched. I would hesitate to make a shortcut to launch a macro that already has a pre-existing macro assigned to it. If you were to assign your macro to run when you pressed ctrl + c, it would overwrite the copy function it already has.

0


source







All Articles