8

I need to detect paste command of excel. Is there any work around which can tell us when user click the paste on the menu pop up from the left mosue button click. This is required me to execute a procedure if user click the paste menu item. Any help would be appreciated.

Regards, Amit

Ben Rhys-Lewis
  • 3,118
  • 8
  • 34
  • 45
Amit
  • 269
  • 2
  • 5
  • 10
  • 2
    See this on how to trap any paste event http://www.siddharthrout.com/2011/08/15/vba-excelallow-paste-special-only/ – Siddharth Rout Sep 21 '12 at 09:44
  • @SiddharthRout the link you've provided is now giving `No Results Found` error. Can you please help us with the updated link? – RBT Feb 21 '18 at 11:34

1 Answers1

19

Borrowing from Excel VBA How to detect if something was pasted in a Worksheet. The Workbook_SheetChange event will fire for any change event on the page, including a paste.

From inside this event, you can then check if the last change was a paste by looking at the most recent entry in the Undo List History:

Private Sub Workbook_SheetChange(ByVal Sh As Object, ByVal Target As Range)
  Dim lastAction As String

  ' Get the last action performed by user
  lastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)

  ' Check if the last action was a paste
  If Left(lastAction, 5) = "Paste" Then

    ' Do Stuff Here

  End If
End Sub
Community
  • 1
  • 1
KyleMit
  • 30,350
  • 66
  • 462
  • 664
  • nice solution for cell contents. Do You know, if there is a way to detect the paste-event of an image or similar objects? – DrMarbuse Mar 02 '21 at 09:03