Autofilter TextBox with Multiple ActiveX Option Buttons

sdennant

New Member
Joined
Mar 17, 2020
Messages
41
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I can't seem to wrap my head around this... Basically i have create a worksheet with a textbox whereby when i search for a word it autofilters a selected column. I also have three option buttons that in theory, i would want a user to select whichever button and that would dictate where that textbox searches. I only have one text box and three different buttons.

What i found that happens is, if a user selects one button and types, it filters correctly, then if you select another one it keeps the first filter and applies a second. but i actually want it only to filter one at a time if that makes sense?

I've also attached a screenshot of the search bar textbox i have created.

Here's the code:

VBA Code:
Private Sub NameTextBox_DropButtonClick()

If ActiveSheet.OptionButton1 = True Then
    ActiveSheet.ListObjects("Names").Range _
    .AutoFilter Field:=19, Criteria1:="=*" & NameTextBox.Text & "*"
    Else
        End If

If ActiveSheet.OptionButton2 = True Then
    ActiveSheet.ListObjects("Names").Range _
    .AutoFilter Field:=17, Criteria1:="=*" & NameTextBox.Text & "*"
    Else
        End If
If ActiveSheet.OptionButton3 = True Then
    ActiveSheet.ListObjects("Names").Range _
    .AutoFilter Field:=18, Criteria1:="=*" & NameTextBox.Text & "*"
    Else
        End If
    
End Sub
 

Attachments

  • Capture excel.PNG
    Capture excel.PNG
    4.1 KB · Views: 8

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
Before each filter, switch off autofiltering and then back on again. That will clear the previous filter.

VBA Code:
ActiveSheet.AutoFilterMode = False
ActiveSheet.AutoFilterMode = True
 
Upvote 0
Thanks for your message CountTepes, but it throws up a few errors. Using the above code do you know where exactly I should place this new code?
 
Upvote 0
Before each filter, switch off autofiltering and then back on again. That will clear the previous filter.

VBA Code:
ActiveSheet.AutoFilterMode = False
ActiveSheet.AutoFilterMode = True

Thanks for your message CountTepes, but it throws up a few errors. Using the above code do you know where exactly I should place this new code
 
Upvote 0
To clear the filter for a ListObject, try...

VBA Code:
    With ActiveSheet.ListObjects("Names")
        If .ShowAutoFilter Then
            With .AutoFilter
                If .FilterMode Then .ShowAllData
            End With
        End If
    End With

However, your macro can be re-written as follows...

Code:
Private Sub NameTextBox_DropButtonClick()

    Dim objListObject As ListObject
    Set objListObject = ActiveSheet.ListObjects("Names")

    With objListObject
        If .ShowAutoFilter Then
            With .AutoFilter
                If .FilterMode Then .ShowAllData
            End With
        End If
    End With
  
    Dim fieldNumber As Long
    If ActiveSheet.OptionButton1 Then
        fieldNumber = 19
    ElseIf ActiveSheet.OptionButton2 Then
        fieldNumber = 17
    ElseIf ActiveSheet.OptionButton3 Then
        fieldNumber = 18
    End If
  
    objListObject.Range.AutoFilter Field:=fieldNumber, Criteria1:="=*" & NameTextBox.Text & "*"
        
End Sub

Hope this helps!
 
Upvote 0
To clear the filter for a ListObject, try...

VBA Code:
    With ActiveSheet.ListObjects("Names")
        If .ShowAutoFilter Then
            With .AutoFilter
                If .FilterMode Then .ShowAllData
            End With
        End If
    End With

However, your macro can be re-written as follows...

Code:
Private Sub NameTextBox_DropButtonClick()

    Dim objListObject As ListObject
    Set objListObject = ActiveSheet.ListObjects("Names")

    With objListObject
        If .ShowAutoFilter Then
            With .AutoFilter
                If .FilterMode Then .ShowAllData
            End With
        End If
    End With
 
    Dim fieldNumber As Long
    If ActiveSheet.OptionButton1 Then
        fieldNumber = 19
    ElseIf ActiveSheet.OptionButton2 Then
        fieldNumber = 17
    ElseIf ActiveSheet.OptionButton3 Then
        fieldNumber = 18
    End If
 
    objListObject.Range.AutoFilter Field:=fieldNumber, Criteria1:="=*" & NameTextBox.Text & "*"
       
End Sub

Hope this helps!

You're an absolute genius mate, thank you. This forum is amazing.

I have one more request, is it possible that after each search result it goes back to the top of the page? As of now, it filters perfectly across all buttons/columns but once i search a second it depending on how large the first result is to the second, it looks like it's blank cells but really we just need to scroll to the top.

I'm trying to make this as user friendly as possible for people that dont know how to use excel that much.

Also, one more thing and this is okay if not but i have another code attached to the search button which essentially calls the NameTextBox_DropButtonClick() but is there a way to search without that button and hit enter/return? If i didnt include that button, the dropbuttonclick was called by hitting F4.

Wishing you safety during this time.

best,
Steven
 
Upvote 0
To scroll to the top, try...

VBA Code:
activesheet.range("A1").select
 
Upvote 0
How about designating your search button as the default command button? This way the user can either click on the button or press ENTER to trigger the command button Click event (assuming that you have no other command buttons or, if you do, no other command button has focus).

If this will work for you, first move your code to the command button's Click event...

VBA Code:
Private Sub CommandButton1_Click() 'change the name accordingly
    'your code here
    '
    '
End Sub

Then, simply set the Default property for the command button to True.
 
Last edited:
Upvote 0
Of course, one can always Tab over to the search button, and then press ENTER.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,143
Messages
6,123,276
Members
449,093
Latest member
Vincent Khandagale

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top