繁体   English   中英

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

[英]EXCEL VBA limit copy and paste to current column for data validation columns

我有一个很大的电子表格,并且对具有下拉列表的几列进行了验证。

我在下面的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的硕士,这就是为什么我问你。

通过Excel VBA中存在的代码, 如何检测工作表中是否粘贴了某些内容,您可以对其进行修改以使其具有类似以下内容:

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并捕获所需的内容。

HTH;)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM