在excel中复制粘贴操作时禁用格式设置



我有以下代码可以在单元格复制粘贴操作时禁用格式化-

Private Sub Worksheet_Change(ByVal Target As Range)
With Application
.EnableEvents = False
myValue = Target.Formula
.Undo
Target.Formula = myValue
.EnableEvents = True
End With
End If
Application.CutCopyMode = False
End Sub

代码运行得很好,但它在工作表中插入了许多其他问题。

  1. 无法使用撤消/重做功能
  2. 无法在单击中更改单元格的焦点

如有任何想法,不胜感激。

本质上,您希望禁止标准粘贴,并可能用特殊粘贴/值替换它

您可以捕获粘贴功能,并分配一条消息,告诉用户使用粘贴特殊/值,就像在中一样

....
' place this in any suitable event trigger like 
Application.CommandBars("Edit").Controls("Paste").OnAction = "TrappedPaste"
....
Sub TrappedPaste()
MsgBox "Your Paste is performed as PasteSpecialValues", vbOKOnly, "Paste"
' ok, now silently do a PasteSpecial/Values
On Error GoTo TryExcel
' try to paste text
ActiveSheet.PasteSpecial Format:="Text", Link:=False, DisplayAsIcon:=False
Exit Sub
TryExcel:
On Error GoTo DoesntWork
Selection.PasteSpecial xlPasteValues
Exit Sub
DoesntWork:
MsgBox "Sorry - wrong format for pasting", vbExclamation + vbOKOnly, "Paste Problem"
End Sub

小心。。。这并不适用于所有语言,因此对于国际应用程序,我们需要更微妙的

If ExistControl("Edit", 22) Then Application.CommandBars("Edit").FindControl(ID:=22).OnAction = "TrappedPaste"

在应用程序中还有更多的地方,用户可以从中获得"粘贴",您需要捕获所有这些地方。

如果你喜欢这种方法,我可以进一步详细说明。

最新更新