我在尝试查找单元格中的值是否与定义单元格下拉列表的命名范围中的值列表匹配。Excel VBA .find在不匹配时匹配
我的问题是,如果用户在单元格中输入星号,则此值不是有效的下拉值,但会验证列表中的第一项。在下面的代码中,如果szCellValue =“*”,那么验证不起作用。
有谁知道如何让这个搜索工作?
范围值
DESK
现场
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
只是一个问题,为什么你的代码验证,如果输入值的范围是因为你已经有一个数据验证列表? –
你可以用'〜'来转义星号。例如:'szCellValue = Replace(szCellValue,“*”,“〜*”)' –
@TimWilliams我认为你应该将它作为答案发布,它正确回答问题并且有用,不是吗? –