vba 如何检测excel中的粘贴事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12525942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to detect paste event in excel
提问by Amit
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.
我需要检测excel的粘贴命令。是否有任何解决方法可以告诉我们何时用户单击左侧鼠标按钮单击弹出的菜单上的粘贴。如果用户单击粘贴菜单项,则需要我执行一个过程。任何帮助,将不胜感激。
Regards, Amit
问候, 阿米特
回答by KyleMit
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.
借用Excel VBA 如何检测是否在工作表中粘贴了某些内容。该Workbook_SheetChange
事件将针对页面上的任何更改事件触发,包括粘贴。
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