VBA新手在这里。
我有一个excel电子表格,已锁定格式化。但是,如果粘贴到电子表格中,则复制的格式将粘贴到锁定的工作表中。我使用下面的代码在excel中创建一个事件来撤消和粘贴特殊值。
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实例中使用它?
谢谢! 丹
答案 0 :(得分:-1)
将其粘贴到同一文件的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