我需要你的帮助; 我有这两个代码: 第一个是禁用复制过去的宏
Sub Desable_Copy()
Dim oCtrl As Office.CommandBarControl
For Each oCtrl In Application.CommandBars.FindControls(ID:=21)
oCtrl.Enabled = False
Next oCtrl
For Each oCtrl In Application.CommandBars.FindControls(ID:=19)
oCtrl.Enabled = False
Next oCtrl
Application.CellDragAndDrop = False
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
With Application
.CellDragAndDrop = False
.CutCopyMode = False 'Clear clipboard
End With
End Sub
第二个是启用复制过去的宏:
Sub Enable_Copy()
Dim oCtrl As Office.CommandBarControl
For Each oCtrl In Application.CommandBars.FindControls(ID:=21)
oCtrl.Enabled = True
Next oCtrl
For Each oCtrl In Application.CommandBars.FindControls(ID:=19)
oCtrl.Enabled = True
Next oCtrl
Application.CellDragAndDrop = True
End Sub
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
With Application
.CellDragAndDrop = True
.CutCopyMode = True 'Clear clipboard
End With
End Sub
当我执行代码时,我收到一条错误消息:"检测到模糊名称"
任何想法请!!
答案 0 :(得分:2)
为Excel应用程序设置Excel的复制/粘贴功能。如果为一个工作簿禁用它们,则会禁用所有工作簿。如果您同时打开多个工作簿,那么管理就变得非常繁琐 - 如果您是专家程序员,也许您不是。考虑替代方法,例如可以在Worksheet_Change事件上运行的Application.Undo
。以下代码将撤消工作表上的任何粘贴操作。
Private Sub Worksheet_Change(ByVal Target As Range)
' 18 Apr 2017
Dim UndoList As String
With Application
.EnableEvents = False
.ScreenUpdating = False
End With
On Error GoTo ErrExit
UndoList = Application.CommandBars("Standard").Controls("&Undo").List(1)
If Left(UndoList, 5) = "Paste" Or UndoList = "Auto Fill" Then
MsgBox "Please don't paste values on this sheet." & vbCr & _
"The action will be reversed.", vbInformation, _
"Paste is not permitted"
With Application
.Undo
.CutCopyMode = False
End With
Target.Select
End If
ErrExit:
With Application
.ScreenUpdating = True
.EnableEvents = True
End With
End Sub
此代码改编自code published here。这里采取的视图不是为了防止粘贴操作,而是为了防止粘贴操作弄乱页面格式。这是一篇非常有趣的文章,解释清楚,易于实施。
答案 1 :(得分:0)
你有两个同名的私人子公司。
例如,您可以更改第二个:
Property
到
Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)