簡體   English   中英

Excel中的命名區分大小寫驗證

[英]Case sensitive validation in Excel from Named List

我在Excel 2003中使用VBA來應用驗證,以將驗證應用於命名列表中給定范圍的單元格。 然后,用戶可以從值的下拉列表中進行選擇。

編輯:給定名為“ 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

一切正常,但問題是從命名列表應用驗證時,它不區分大小寫。 即,如果下拉選項為“ John Smith”,則用戶可以在已驗證的單元格中鍵入“ john smith”或“ john SmiTh”,Excel仍會將其視為有效條目。

我知道通過“工具->驗證...”手動創建列表將使查找驗證區分大小寫,但就我而言,這是不可行的-我必須填充命名列表並以編程方式分配驗證。

有誰知道一種確保基於命名列表的Excel驗證區分大小寫的方法?

謝謝。

好吧,您可以根據給定的驗證范圍來構建驗證列表(假設它不是太大)

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

看看這個:

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

我還沒有測試它,但是它有點復雜,但是我認為它可以滿足您的要求。

怎么樣? 如果使用vbBinaryCompare,則StrComp字符串比較區分大小寫。 例如:

   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