Fork me on Gitee

禁止EXCEL单元格粘贴和拖放,只允许手动输入

方式一:
Option Explicit
Private Sub Worksheet_SelectionChange(ByVal Target  As Range)
If Not Application.Intersect(Target, Range("F10:M26,D2")) Is Nothing Then
MsgBox ("禁止粘贴内容!,请双击后输入内容!")
Application.CellDragAndDrop = False
Else
Application.CellDragAndDrop = True
End If
End Sub
Private Sub Worksheet_Deactivate()
Application.CellDragAndDrop = True
End Sub

 注意:Range("F10:M26,D2") 为指定的禁止粘贴的区域。只能手动输入。

方式二:

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Row > 1 And Target.Row < 1000000 And (Target.Column = 2 Or Target.Column = 4) Then

    On Error Resume Next
    Application.EnableEvents = False
    If CommandBars.FindControl(, 128).List(1) = Left(CommandBars.FindControl(, 22).Caption, 2) Then
        Application.Undo
        MsgBox "请勿使用粘贴!,请手动输入或下拉菜单", vbExclamation
    End If
    Application.EnableEvents = True
End If
End Sub

 注意:Target.Row > 1 And Target.Row < 1000000 And (Target.Column = 2 Or Target.Column = 4)为指定的禁止粘贴的区域。表示从第1行到1000000 并且是第二列第四列只能手动输入或下拉菜单。

 

posted @ 2021-07-30 00:28  JoePotter  阅读(1154)  评论(0编辑  收藏  举报
``