简体   繁体   English

Excel中的命名区分大小写验证

[英]Case sensitive validation in Excel from Named List

I'm using VBA in Excel 2003 to apply validation to apply validation to a given range of cells from a named list. 我在Excel 2003中使用VBA来应用验证,以将验证应用于命名列表中给定范围的单元格。 The user can then select from a dropdown list of values. 然后,用户可以从值的下拉列表中进行选择。

Edit: Here's how I'm setting the validation, given a named range called 'MyLookupList' 编辑:给定名为“ MyLookupList”的命名范围,这就是我设置验证的方式

        With validatedRange.Validation
            .Delete
            .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
                Operator:=xlBetween, Formula1:="=MyLookupList"
            .ErrorMessage = "Invalid value. Select one from the dropdown list."
            .InCellDropdown = True
        End With

All that works fine, but the problem is that when validation is applied from a named list, it is case-insensitive. 一切正常,但问题是从命名列表应用验证时,它不区分大小写。 Ie if a dropdown choice is "John Smith", then the user can type in "john smith" or "john SmiTh" into the validated cell and Excel will still treat it as a valid entry. 即,如果下拉选项为“ John Smith”,则用户可以在已验证的单元格中键入“ john smith”或“ john SmiTh”,Excel仍会将其视为有效条目。

I know that manually creating a list via Tools-->Validation... will make the lookup validation case sensitive, but for my case this is just not feasible - I have to populate the named lists and assign validation programmatically. 我知道通过“工具->验证...”手动创建列表将使查找验证区分大小写,但就我而言,这是不可行的-我必须填充命名列表并以编程方式分配验证。

Does anyone know of a way to ensure that Excel validation based on named lists is case-sensitive? 有谁知道一种确保基于命名列表的Excel验证区分大小写的方法?

Thanks. 谢谢。

Well you could just build the validation list given the validation range (assuming it's not too large) 好吧,您可以根据给定的验证范围来构建验证列表(假设它不是太大)

Dim sValidationList As String
Dim iRow As Integer

  'build comma-delimited list based on validation range
  With oValidationRange
    For iRow = 1 To .Rows.Count
      sValidationList = sValidationList & .Cells(iRow, 1) & ","
    Next
  End With

  'trim trailing comma   
  sValidationList = Left(sValidationList, Len(sValidationList) - 1)

  'apply validation to data input range
  With oDataRange.Validation
    .Delete
    .Add Type:=xlValidateList, AlertStyle:=xlValidAlertStop, _
      Operator:=xlBetween, Formula1:=sValidationList
    .ErrorMessage = "Invalid value. Select one from the dropdown list."
    .InCellDropdown = True

  End With

Have a look at this: 看看这个:

http://www.contextures.com/xlDataVal14.html http://www.contextures.com/xlDataVal14.html

I haven't tested it and it's a bit more complicated but I think it will do what you want. 我还没有测试它,但是它有点复杂,但是我认为它可以满足您的要求。

How about StrComp? 怎么样? StrComp string comparison is case sensitive if you use vbBinaryCompare. 如果使用vbBinaryCompare,则StrComp字符串比较区分大小写。 For example: 例如:

   Set c = Range("MyLookupList").Find(Range("ValidateRange"), _
         LookIn:=xlValues)
    If Not c Is Nothing Then
        If StrComp(c, Range("ValidateRange"), vbBinaryCompare) = 0 Then
            'Match '
            MsgBox "OK"
        Else
            MsgBox "Problem"
        End If
    End If

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

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