没有匹配时,Excel VBA .find匹配

时间:2017-07-10 14:20:49

标签: excel vba excel-vba

我试图查找单元格中的值是否与定义单元格下拉列表的命名范围中的值列表匹配。

我的问题是,如果用户在单元格中输入了一个星号,则该值不是有效的下拉值,而是验证列表中的第一个项目。在下面的代码中,如果szCellValue =" *"然后验证不起作用。

有谁知道如何让这个搜索工作?

范围值

DESK

ON-SITE

N / A

确定匹配的代码

Dim bError As Boolean

Dim oCell As Range
Dim oFoundCell As Range

Dim szCellValue As String
Dim szLookupValue As String

szCellValue = CStr(Trim(oCell.Value2))

' Validate In Dropdown if Length > 0
If Len(szCellValue) > 0 Then
    ' See if the oCell value in the oRange loop exists in this szValidationNamedRange dropdown
    Set oFoundCell = GetRangeFromNamedRange(cValidateCellData.ValidationNamedRange).Find(szCellValue, LookIn:=xlValues, Lookat:=xlWhole)

    ' If Value Not Found in Dropdown...or if they've typed in an id value (which will be found on odd numbered columns)
    If oFoundCell Is Nothing Then
        Call SetError(oCell.Text, cValidateCellData, "Not a Valid Value for drop down " + cValidateCellData.ValidationNamedRange + ".")
        bError = True
    End If

Else
    If cValidateCellData.Required Then
        Call SetError(oCell.Text, cValidateCellData, "Please input a value. This is a Required Field.")
    End If
End If

1 个答案:

答案 0 :(得分:1)

您可以使用〜来转义星号。

例如:

Double