我正在寻找一种使用 VBA 捕获 Excel 中标准工具栏工具的单击的方法。主要需求是捕获用户的任何复制、剪切和粘贴请求。我可以捕获这些功能的热键,但当用户单击标准工具栏上的图标或用户使用菜单选择功能时,我找不到捕获相同功能的方法。
答案1
以下是 John Walkenbalk 的撤消示例 http://www.j-walk.com/ss/excel/tips/tip23.htm
我还使用了宏(由 worksheet_change 事件调用)来强制用户“选择性粘贴”。它应该为您提供一个很好的起点。
Sub pastefix()
Dim UndoString As String
Dim srce As Range
On Error GoTo err_handler
UndoString = _
Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(UndoString, 5) <> "Paste" And UndoString <> "Auto Fill" Then
Exit Sub
End If
Application.ScreenUpdating = False
Application.EnableEvents = False
Application.Undo
If UndoString = "Auto Fill" Then
Set srce = Selection
srce.Copy
ActiveCell.PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
Union(ActiveCell, srce).Select
Else
ActiveCell.PasteSpecial _
Paste:=xlPasteValues, _
Operation:=xlNone, _
SkipBlanks:=False, _
Transpose:=False
End If
Application.CutCopyMode = False
Application.ScreenUpdating = True
Application.EnableEvents = True
Exit Sub
err_handler:
Application.ScreenUpdating = True
Application.EnableEvents = True
Application.CutCopyMode = False
End Sub