如何在excel中的所有打开的会话中运行启用excel的事件macros?

VBA新手在这里。

我有一个Excel格式的电子表格被locking格式。 但是,如果您粘贴到电子表格中,则复制的格式将被粘贴到locking的工作表中。 我使用下面的代码在Excel中创build一个事件来撤销和粘贴特殊值。

Private Sub Worksheet_Change(ByVal Target As Excel.Range) If Application.CutCopyMode = xlCopy Then Application.EnableEvents = False Application.Undo Target.PasteSpecial Paste:=xlPasteValues Application.EnableEvents = True End If End Sub 

这完美的作品,但是,这只适用于如果我复制和粘贴在同一个Excel会话。 我怎样才能让这个工作跨所有的Excel实例?

谢谢! 担

将其粘贴到相同文件的Thisworkbook模块中(假设模块当前为空):

 Option Explicit Private WithEvents App As Application Private Sub App_SheetChange(ByVal Sh As Object, ByVal Target As Range) If Application.CutCopyMode = xlCopy Then Application.EnableEvents = False Application.Undo Target.PasteSpecial Paste:=xlPasteValues Application.EnableEvents = True End If End Sub Private Sub Workbook_BeforeClose(Cancel As Boolean) Set App = Nothing End Sub Private Sub Workbook_Open() Set App = Application End Sub