简体   繁体   English

Excel 搜索栏上的宏复杂问题 Function

[英]Excel Macro Complex Question on Search Bar Function

My specific Excel file我的特定 Excel 文件

Hello, I have all the correct buttons and set up for what I believe to make my excel sheet searchable.您好,我拥有所有正确的按钮并设置了我认为可以让我的 excel 工作表可搜索的内容。 I think there is an error in my code on the VBA on the selector values such as my range.我认为在 VBA 上的选择器值(例如我的范围)上的代码中存在错误。 Although I have range A6 to Y1000 selected, and my search box is C3, shouldn't this run?虽然我选择了 A6 到 Y1000 的范围,而且我的搜索框是 C3,但这不应该运行吗?

The set up says my radio buttons needed to be named the exact same as the column name, thus the weird names.设置说我的单选按钮需要与列名完全相同,因此名称很奇怪。 I would do a search bar with no radio buttons at all, just search for anything matching on the sheet but that seemed too advanced.我会做一个根本没有单选按钮的搜索栏,只搜索工作表上匹配的任何内容,但这似乎太高级了。

Does anyone know what specifically in my code is incorrect to run on a set up spreadsheet like so?有谁知道我的代码中的具体内容在这样的设置电子表格上运行是不正确的吗? 在此处输入图像描述

    Sub SearchBox()
    'PURPOSE: Filter Data on User-Determined Column & Text
    'SOURCE: www.TheSpreadsheetGuru.com

    Dim myButton As OptionButton
    Dim MyVal As Long
    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("A6:Y1000") 'Cell Range
      'Set DataRange = sht.ListObjects("Sheet1").Range 'Table

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

    'Loop Through Option Buttons
      For Each myButton In ActiveSheet.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:="=*" & mySearch & "*", _
        Operator:=xlAnd
      
    'Clear Search Field
      sht.Shapes("UserSearch").TextFrame.Characters.Text = "" 'Control Form
      'sht.OLEObjects("UserSearch").Object.Text = "" 'ActiveX Control
      'sht.Range("A6").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

I either have nothing happen, or get an error code like this one:我要么什么都没有发生,要么得到一个像这样的错误代码: 在此处输入图像描述

A reference on where I got most of the VBA code from 关于我从哪里获得大部分 VBA 代码的参考

You Need to remplace you text field by a ActiveX Textbox您需要用 ActiveX 文本框替换您的文本字段

在此处输入图像描述

And access if like this:如果像这样访问:

Sheets("Feuil1").TextBox1.Text = ""

You need to replace you searching line too:您也需要替换您的搜索行:

mySearch = Sheets("Feuil1").TextBox1.Text

I've updated your code, just DON'T FORGET to update your sheetname我已经更新了您的代码,只是不要忘记更新您的工作表名称

Sub SearchBox()
    'PURPOSE: Filter Data on User-Determined Column & Text
    'SOURCE: www.TheSpreadsheetGuru.com

    Dim myButton As OptionButton
    Dim MyVal As Long
    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("A6:Y1000") 'Cell Range
      'Set DataRange = sht.ListObjects("Sheet1").Range 'Table

    'Retrieve User's Search Input
      mySearch = Sheets("Feuil1").TextBox1.Text 'Control Form
      'mySearch = sht.OLEObjects("UserSearch").Object.Text 'ActiveX Control
      'mySearch = sht.Range("A1").Value 'Cell Input

    'Loop Through Option Buttons
      For Each myButton In ActiveSheet.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:="=*" & mySearch & "*", _
        Operator:=xlAnd
      
    'Clear Search Field
      Sheets("Feuil1").TextBox1.Text = "" 'Control Form
      'sht.OLEObjects("UserSearch").Object.Text = "" 'ActiveX Control
      'sht.Range("A6").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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM