Excel VBA .find匹配时不匹配

我正在尝试查找单元格中的值是否与定义单元格下拉列表的指定范围内的值列表匹配。

我的问题是,如果用户在单元格中input一个星号,这个值不是一个有效的下拉值,但它validation到列表中的第一个项目。 在下面的代码中,如果szCellValue =“*”,那么validation不起作用。

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

范围值

现场

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 

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

例如:

 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 ' (escape * using ~) Set oFoundCell = GetRangeFromNamedRange(cValidateCellData.ValidationNamedRange) _ .Find(Replace(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