Filter the data using SEARCH BOX

  • Below mentioned code enunciates how to filter the rows based on criteria mentioned in SEARCHBOX
  • We can consider searchbox in two ways

  • (i)Consider a cell as serachbox
    (ii)Consider a Textbox as searchbox


  • Here i provided the code for these two scenarios by using change event of Worksheet and Textbox respectively
  •  

  • Click on below mentioned image to watch video
  • Download The Workbook

     

     

  • Change event code when TextBox considered as SEARCH BOX
  • Private Sub TextBox1_Change()

    'Declare a Variable for sheet2
    Dim Sh As Worksheet
    Set Sh = ActiveWorkbook.Sheets("Sheet2")

    'Declare a variable for data exists in Textbox
    Dim FilterData As String
    FilterData = Sh.Shapes("TextBox1").OLEFormat.Object.Object.Value

    'If change happend in the defined range then underlying code will execute
    Sh.Range("A5").AutoFilter field:=Sh.Range("C2").Value, Criteria1:="*" & FilterData & "*", Operator:=xlFilterValues

    End Sub

     

     

  • Change event code when Cell considered as SEARCH BOX
  • Private Sub Worksheet_Change(ByVal Target As Range)

    'Declare a Object variable for sheet2
    Dim Sh As Worksheet
    Set Sh = ActiveWorkbook.Sheets("Sheet2")

    'Declare a Object variable to define a range
    Dim CellRng As Range
    Set CellRng = Intersect(Target, Sh.Range("C3"))

    'If change happend in the defined range then underlying code will execute
    If Not CellRng Is Nothing Then
    Sh.Range("A5").AutoFilter field:=Sh.Range("C2").Value, Criteria1:="*" & Sh.Range("C3").Value & "*", Operator:=xlFilterValues
    End If

    End Sub

     

    Faq: Importance of DESIGN MODE of CONTROL

     

     

    Faq: Wild Character STAR