"Contains" filter on number and text column

tomleitch

Board Regular
Joined
Jan 3, 2012
Messages
189
Hi,

I've got a column that I want to filter using "contains" filter in my vba code.

Most of the column data is a 3 digit number - e.g. "842"
But some have more digits/letters - e.g. "84203-A"

Filter input is from a textbox in a userform.

Is there any way I can use an autofilter so that if "842" is entered in the textbox that it will pick up "842" and "84203-A" (i.e. anything that contains "842" at the beginning of it?


Many thanks
Tom
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Doesn't seem to work.

The code I have in my userform is:

Code:
ActiveWorkbook.Worksheets("OPS PLANNER").Range(Cells(2, NOTES), Cells(Lrow, NOTES)).AutoFilter Field:=Sys, Criteria1:=TextBox1.Value

I've also tried:
Code:
ActiveWorkbook.Worksheets("OPS PLANNER").Range(Cells(2, NOTES), Cells(Lrow, NOTES)).AutoFilter Field:=Sys, Criteria1:="*" & TextBox1.Value & "*"

And neither work.

The first one works, but only brings back an exact match, i.e. filters 842 but not 84201-A or whatever other variant.


Cheers
Tom
 
Upvote 0
How about identifying the cell by using the Instr function within VBA, ie.

Code:
If instr(Range("X" & i, "842")) > 0 then
SomeAction

Where X is the column and i is a variable indicating the row
 
Upvote 0
See if this works for you

Rich (BB code):
Dim aCrit As Variant

With ActiveWorkbook.Worksheets("OPS PLANNER").Range(Cells(2, Notes), Cells(Lrow, Notes))
  aCrit = Filter(Application.Transpose(.Value), TextBox1.Value, True)
  .AutoFilter Field:=Sys, Criteria1:=aCrit, Operator:=xlFilterValues
End With
 
Upvote 0

Forum statistics

Threads
1,214,641
Messages
6,120,693
Members
448,979
Latest member
DET4492

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