简体   繁体   English

Excel VBA验证对象

[英]Excel VBA Validation Object

A VBA feature that I haven't use and need advice on. 我尚未使用且需要建议的VBA功能。

I have a large, hideous sheet that needs data validation upon data entry. 我有一个大而丑陋的表,需要在输入数据时进行数据验证。 Lots of typos and bad numbers to fix. 许多错别字和错误的数字需要解决。 I just started playing with the Validation object and need advice... 我刚刚开始使用Validation对象,需要建议...

  • I'm a little wary of using the interface-based Data Validation, especially from a maintenance perspective. 我对使用基于接口的数据验证有些警惕,尤其是从维护角度而言。 Is that silly? 那很傻吗?

  • Are Ranges (or anything else) allowed to have more than one validation object? 是否允许范围(或其他任何东西)具有多个验证对象?

  • Is there a simple way to trigger the validation object upon cell entry, or is that just the same as using the interface-based Validation? 是否有一种简单的方法可以在单元格进入时触发验证对象,或者与使用基于接口的验证相同?

  • Am I making a big deal out of nothing? 我一无所有吗?

Thanks, Mike 谢谢,迈克

Taking your questions in order 1. Using the inbuilt Data validation is the easiest and simplest method to go with but it does have limitations. 按顺序处理您的问题1.使用内置的数据验证是最容易和最简单的方法,但它确实有局限性。 You are not being silly, but the answer depends on your situation, taking several factors into account such as "how often do you anticipate the validations might change", "how complex are the validations", "how many different users might be using this sheet"? 您并不傻,但是答案取决于您的情况,并考虑了以下几个因素:“您预期验证的频率会改变多少”,“验证的复杂程度”,“有多少不同的用户正在使用此功能”片”? (I find the more users you have, the more likely one of them will do something different and "break" your validations. The biggest issue I have encountered with in cell Data validation is that is is susceptible to users copying cells and pasting over the top of your validations and this removes the validation (without the user or you realising it) 2. As far as I know, you cannot have more than 1 data validation per cell 3. I no longer use the below method, and came up with my own custom data validation method, but the code below effectively reruns the in-cell Data validation checks. This procedure is called for our wider Validatiosn module, which is run at the end of data entry, so it isn't run in "real time" (我发现您拥有的用户越多,他们中的一个就越有可能做一些不同的事情并“破坏”您的验证。我在单元格数据验证中遇到的最大问题是,容易受到用户复制单元格并粘贴到验证的顶部,这样就删除了验证(没有用户或您没有意识到)。2.据我所知,每个单元格不能有超过1个数据验证。3.我不再使用下面的方法,并且想出了我自己的自定义数据验证方法,但是下面的代码有效地重新运行了单元内数据验证检查。此过程被称为我们的更广泛的Validatiosn模块,该模块在数据输入的末尾运行,因此它不是在“真实”环境中运行时间”

Sub copy_paste_errors_CO()

If Application.Version >= "12.0" Then
Application.StatusBar = "Checking for Cell Format errors"
' This will work for excel 2007 but not earlier
'--------- Identify Copy and Paste errors where cell level validation not activated
    Dim DataRange As Range
    Dim c As Range
    Dim count As Long
    Dim ExactMatch As Boolean
    'Find the cells containing Data Validation
    Set DataRange = Worksheets("Contacts").Range(CO_DataRange).SpecialCells(xlCellTypeAllValidation)
    If DataRange Is Nothing Then
        MsgBox "no cells with any validation"
    Else
        'Loop through each cell that has data validation
        For Each c In DataRange
            If Not IsError(c.Value) Then 'If the data validation errors
                If Len(c.Value) > 0 Then ' only check if data in cell '
                    ExactMatch = False
                    If c.Validation.Type <> xlValidateList Then
                        If c.Validation.Value Then
                            ExactMatch = True
                        End If
                    Else   'List validation
                        Dim LookupName, MatchValue As String
                        Dim rng As Range
                        LookupName = c.Validation.Formula1
                        LookupName = Right(LookupName, Len(LookupName) - 1)
                        Set rng = Range(LookupName).Find(c.Value, LookIn:=xlValues, lookat:=xlWhole)
                        If Not rng Is Nothing Then
                            If StrComp(rng, c.Value, vbBinaryCompare) = 0 Then
                                ExactMatch = True
                            End If
                        End If
                    End If
                    If Not ExactMatch Then
                        'If there is an error, store the error in the error array and colour the interior of the failing cell
                        CopyAndPasteCount_CO = CopyAndPasteCount_CO + 1
                        CopyAndPasteResultsArray(CopyAndPasteCount_CO, 1) = c.Row - CO_StartRow + 1
                        CopyAndPasteResultsArray(CopyAndPasteCount_CO, 2) = c.Address
                        With c.Interior
                            .ColorIndex = CCellFormatErrorColour
                            .Pattern = xlSolid
                        End With
                    End If
                End If
            End If
        Next
    End If
End If

End Sub
  1. As to if you are making a big deal out of nothing, this is linked to answer 1. If you have a limited number of uses, who know how the sheet works and some of its pitfalls, then in-cell data validation may be sufficient for your needs 关于您是否一无所获,这与答案1相关联。如果您的使用次数有限,并且知道工作表的工作原理和某些陷阱,那么单元内数据验证就足够了满足您的需求

The solution I now use for data validation is to define a "template" sheet for each sheet I want to validate and within that I define the validation rules for each column. 我现在用于数据验证的解决方案是为要验证的每个工作表定义一个“模板”工作表,并在其中为每一列定义验证规则。 This has the benefits of not being corruptible by a user copying and pasting, and also allows more than 1 validation per column. 这样的好处是不会被用户复制和粘贴损坏,并且每列允许进行1个以上的验证。 My code is parameterized, so the same code works across multiple sheets and indeed multiple different workbooks, simply by adhering to the same template structure. 我的代码是参数化的,因此只需遵循相同的模板结构,即可在多个工作表甚至多个不同的工作簿中使用相同的代码。 This method is also several magnitudes faster than the original code pasted above, so I wont include it here unless you think it is something you would like to consider 这种方法也比上面粘贴的原始代码快几个数量级,因此除非您认为它是您要考虑的东西,否则我不会在此处包括它

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

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