Run Macro when user clicks Paste or Paste Special

Bill Bisco

Active Member
Joined
Aug 8, 2007
Messages
446
Dear all,

I am currently using this code to run a macro when a user types V

Code:
Application.OnKey "^v", "PasteSizedPicture"

I'd like to run the same Macro "PasteSizedPicture" whenever a user clicks Paste or Paste Special instead of the regular action. Is there a method to do this?

Thanks,

Bill
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college
You could try this code in the worksheet module assuming you know which column the paste will be made to, in this example column A

Code:
Private Sub Worksheet_Change(ByVal Target As Range)


If Target.Column = 1 Then Call PasteSizedPicture


End Sub
 
Upvote 0
Paste this code in the codepage of the worksheet (not in a module)
Code:
Private Sub Worksheet_Change(ByVal Target As Range)
    'English
    'Const UNDOCMD As String = "&Undo"
    'Const PASTECMD As String = "Paste"
    'Const PASTECMD2 As String = "Paste" '<-- change!
    
    'Dutch
    Const UNDOCMD As String = "Onge&daan maken"
    Const PASTECMD As String = "Plakken"
    Const PASTECMD2 As String = "Opmaak van"
    
    Dim cb      As CommandBars
    Dim cbco    As CommandBarControl
    Dim lastAction As String
    Dim i As Long
    
    ' Get the last action performed by user
    Set cb = Application.CommandBars
    Set cbco = cb("Standard").Controls(UNDOCMD)
    
    If cbco.Enabled Then  'is a command to undo available?
        lastAction = cbco.List(1)
        ' Check if the last action was a paste
        If Left(lastAction, Len(PASTECMD)) = PASTECMD _
        Or Left(lastAction, Len(PASTECMD2)) = PASTECMD2 Then
            PasteSizedPicture
        End If
    End If
End Sub

I work with a dutch version of excel. If you use the english version comment the dutch const lines, uncomment the english const lines and figure out what text the PASTECMD2 looks like (probably something like "Keep format", mouse over the various paste options and look at the text)
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,865
Members
449,052
Latest member
Fuddy_Duddy

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top