使用选项按钮在 excel 中动态搜索完全匹配和部分匹配

Dynamic Searching for exact and partial matches in excel using option buttons

我有以下代码,允许我使用我创建的与 table 标题匹配的选项按钮搜索 table 上的数据。我可以将搜索条件设置为完全匹配或部分匹配。但是,我想要的是能够搜索 table 中的不同列,而不必总是进入 VBA 代码来打开和关闭此选项。即有些列我想要完全匹配,其他我想要部分匹配。

关于我可以在哪里修改下面的代码的任何帮助?

Sub SearchBox()

Dim myButton As OptionButton
Dim SearchString As String
Dim ButtonName As String
Dim sht As Worksheet
Dim myField As Long
Dim DataRange As Range
Dim mySearch As Variant

'Load Sheet into A Variable
  Set sht = ActiveSheet

'Unfilter Data (if necessary)
  On Error Resume Next
    sht.ShowAllData
  On Error GoTo 0

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

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

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

  'change this to =* if you want to search for anything that containts mysearch rather than just mysearch
    SearchString = "=*" & mySearch & "*"

    End If

'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


Sub ClearFilter()
'PURPOSE: Clear all filter rules

'Clear filters on ActiveSheet
  On Error Resume Next
  ActiveSheet.ListObjects(1).AutoFilter.ShowAllData

  On Error GoTo 0

End Sub

修改并尝试以下操作:

Option Explicit

Sub test()

    Dim ws As Worksheet
    Dim SearchValue As String, FullReport As String
    Dim rng As Range, cell As Range

    'What i m looking for
    SearchValue = "Test"

    'Where to look for
    Set rng = ThisWorkbook.Worksheets("Sheet1").UsedRange

    For Each cell In rng

        If cell.Value = SearchValue Then
            If FullReport = "" Then
                FullReport = "The word " & SearchValue & " appears in " & "Column " & cell.Column & ", Row " & cell.Row & "."
            Else
                FullReport = FullReport & vbNewLine & "The word " & SearchValue & " appears in " & "Column " & cell.Column & ", Row " & cell.Row & "."
            End If
        End If

    Next cell

    MsgBox FullReport

End Sub

如果您有固定的列列表,则只需将设置 SearchString 的代码移动到您确定要搜索的列的部分下方,然后根据此列表检查所选字段。但是,我建议把它作为一个单独的函数:

Function getSearchString(searchVal as variant, searchFieldName as string)
    If IsNumeric(searchVal) Then
        getSearchString = "=" & searchVal 
    ElseIf searchFieldName = "MyField1" _
        Or searchFieldName = "MyField2" _
        Or (... List all fields where you want to search partial) Then
        getSearchString = "=*" & searchVal & "*"
    Else
        getSearchString = "=" & searchVal
    End If
End Function

设置变量后调用函数ButtonName

    searchStr = getSearchString(mySearch, ButtonName)

(您当然可以考虑一种更复杂的方法来确定是否使用部分搜索 - 或者可能添加一个复选框让用户选择)