Creating a Macro to Filter based on an Entered Text

Chris Macro

Well-known Member
Joined
Nov 2, 2011
Messages
1,345
Office Version
  1. 365
Platform
  1. Windows
Hi, I am pretty new to macros and have been using the record feature a lot. I have been looking for a way to have a searchbox (which is really just a textbox) that when text is entered in and I click on my macro button that it will filter only what was entered in the searchbox. For example if I have a list of products and suppliers A, B, & C. I want to be able to type in "A" and click the macro button and have it filter to just the "A" suppliers. Thank you for your help!
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
This is the code I currently have:


ActiveSheet.Range("$A$6:$AF$643").AutoFilter Field:=5, Criteria1:= _
"=*A*", Operator:=xlAnd

I believe the blue highlighted text is what needs to be modified. I want it to equal the text entered in "textbox 38"
 
Upvote 0
Assuming your TextBox is from the Control Toolbox:

Code:
    With ActiveSheet
        .Range("$A$6:$AF$643").AutoFilter Field:=5, Criteria1:= _
            "=*" & .OLEObjects("TextBox38").Object.Text & "*", Operator:=xlAnd
    End With
 
Upvote 0
Thanks for your help! Here is what I have in my code. I am getting an error of "expected End Sub". When I put in "End Sub" it gives me the error "Expected End with"

Also, I am just using a regular textbox. For some reason I can not insert a control textbox. Excel will not let me click on the icon.


Code:

Sub Search_Supplier()
With ActiveSheet
.Range("$A$6:$AF$643").AutoFilter Field:=5, Criteria1:= _
"=*" & .OLEObjects("TextBox38").Object.Text & "*", Operator:=xlAnd
End With
 
Upvote 0
For a TextBox from the Drawing Toolbar:

Code:
Sub TTT()
    With ActiveSheet
        .Range("$A$6:$AF$643").AutoFilter Field:=5, Criteria1:= _
            "=*" & .Shapes("Text Box 38").TextFrame.Characters.Text & "*", Operator:=xlAnd
    End With
End Sub

Change the name to suit.
 
Upvote 0
Andrew that worked out great! The only thing I had to change in the code was "text box 38" needed to be "textbox 38". But other than that it works perfectly!! Thank you so much for your time!
 
Upvote 0

Forum statistics

Threads
1,215,161
Messages
6,123,371
Members
449,097
Latest member
thnirmitha

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