首页 文章

EXCEL VBA限制复制并粘贴到数据验证列的当前列

提问于
浏览
0

我有一个大型电子表格,并在几个具有下拉列表的列上进行验证 .

我有以下VBA代码,限制用户点击删除按钮并在下拉列中删除列中的单元格 . 这很好用,但它不会阻止用户从另一列复制单元格并粘贴下拉列表 . 下面是一列的代码 .

Private Sub Worksheet_Change(ByVal Target As Range)
    If Not Intersect(Target, Range("C5:C5004")) Is Nothing Then
        If Len(Target.Text) = 0 Then
            MsgBox "You must select an item from the list!"
            Target.Select
            Application.Undo
        End If
    End If

请告知是否有办法限制复制并粘贴到同一列 .

我正在使用的电子表格是供用户编译大量数据列表,我希望通过下拉列表和长度验证等来维护数据完整性 . 一旦完成,我将采用SSIS加载应用程序各种表中的数据作为速度负载 .

这是我唯一需要的缺失成分 . 我不是VBA的主人,这就是我问你的原因 .

1 回答

  • 0

    Excel VBA How to detect if something was pasted in a Worksheet中的代码中,您可以对其进行调整以获得类似于此的内容:

    Option Explicit
    
    Private Sub Worksheet_Change(ByVal Target As Range)
        Dim lastAction As String
    
        ' Get the last action performed by user
        lastAction = Application.CommandBars("Standard").Controls("&Undo").List(1)
    
        If Not Intersect(Target, Range("C5:C5004")) Is Nothing Then
            ' Check if the cell was cleared or the last action was a paste
            If Len(Target.Text) = 0 Or Left(lastAction, 5) = "Paste" Then
                MsgBox "You must select an item from the list!"
                Target.Select
                Application.Undo
            End If
        End If
    
    End Sub
    

    提示:您还可以检测工作表中执行的其他操作 . 为此,只需将 lastAction 打印到 MsgBoxDebug.Print 并 grab 您需要的那些 .

    HTH;)

相关问题