简体   繁体   English

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

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

I have a large spreadsheet and have validation on several columns that have drop down lists. 我有一个很大的电子表格,并且对具有下拉列表的几列进行了验证。

I have the below VBA code in place that restricts user from hitting the delete button and blanking out cell in column with drop down. 我在下面的VBA代码中设置了适当的位置,以限制用户单击“删除”按钮并使用下拉菜单删除列中的单元格。 This works nicely but it does not prevent user from copying a cell from another column and pasting over the dropdown. 这很好用,但不会阻止用户从另一列复制单元格并粘贴到下拉列表中。 Below is the code for one column. 下面是一栏的代码。

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

Please advise if there is a way to limit copy and paste to the same column. 请告知是否有一种方法可以限制复制和粘贴到同一列。

The spreadsheet that I am working with is for users to compile a large list of data, and I want to maintain data integrity with the drop down lists and validation of lengths etc. Once they are done I will take and using SSIS load the application with data in various tables as a speed load. 我正在使用的电子表格供用户编译大量数据,并且我想通过下拉列表和长度验证等方法保持数据完整性。完成后,我将使用SSIS加载应用程序,并使用各种表中的数据作为速度负载。

This is the only missing ingredient that I am needing. 这是我唯一需要的成分。 I am not a master at VBA which is why I am asking you. 我不是VBA的硕士,这就是为什么我问你。

From the code present in Excel VBA How to detect if something was pasted in a Worksheet you can adapt it to have something similar to this: 通过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

Tip: You can also detect other actions performed in the sheet. 提示:您还可以检测工作表中执行的其他操作。 To do so just print lastAction to a MsgBox or Debug.Print and catch the ones you need. 为此,只需将lastAction打印到MsgBoxDebug.Print并捕获所需的内容。

HTH ;) HTH;)

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

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