Home » excel » vba – how to detect paste event in excel

vba – how to detect paste event in excel

Posted by: admin March 9, 2020 Leave a comment

Questions:

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

How to&Answers:

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