使用VBA在excel中搜索精确匹配

声音

我有下面的代码,正在创建一个搜索栏,其中包含表格上的选项以进行过滤。它工作得很好,但是我似乎无法让它搜索/过滤完全匹配。例如,如果我输入“GE”,它将返回包含“GE”的所有匹配项,而我只需要这两个字母所在的字段。

谁能帮我调整我的代码?

  'Filtered Data Range (include column heading cells)
     'Cell Range
       Set DataRange = sht.ListObjects("Table1").Range 'Table

'Retrieve User's Search Input

  mySearch = sht.OLEObjects("Hello").Object.Text 'ActiveX Control


'Determine if user is searching for number or text
  If IsNumeric(mySearch) = True Then
    SearchString = "=" & mySearch
  Else
    SearchString = "=*" & mySearch & "*"

'Loop Through Option Buttons
  For Each myButton In sht.OptionButtons
    If myButton.Value = 1 Then
      ButtonName = myButton.Text
      Exit For
    End If
  Next myButton

'Determine Filter Field
  On Error GoTo HeadingNotFound
    myField = Application.WorksheetFunction.Match(ButtonName, DataRange.Rows(1), 0)
  On Error GoTo 0

'Filter Data
  DataRange.AutoFilter _
    Field:=myField, _
    Criteria1:=SearchString, _
    Operator:=xlAnd

'Clear Search Field
  'sht.Shapes("UserSearch").TextFrame.Characters.Text = "" 'Control Form
  sht.OLEObjects("Hello").Object.Text = "" 'ActiveX Control
  'sht.Range("A1").Value = "" 'Cell Input

Exit Sub

'ERROR HANDLERS
HeadingNotFound:
  MsgBox "The column heading [" & ButtonName & "] was not found in cells " & DataRange.Rows(1).Address & ". " & _
    vbNewLine & "Please check for possible typos.", vbCritical, "Header Name Not Found!"

End Sub
悉达多溃败

SearchString = "=*" & mySearch & "*" 您正在搜索包含 mySearch 的任何内容。

将其更改为 SearchString = "=" & mySearch

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章