Advance Filtering in VBA with multiple wildcard criterias

Tacty

New Member
Joined
Jul 12, 2022
Messages
7
Office Version
  1. 365
Platform
  1. Windows
Hello everyone. working on a sheet that i would like to use some advance autofilters on with multiple wild cards.
basically when I click the program button it will filter out with wild cards on a specific column.

would like it to filter for

*Honda-CFW-6
*Honda-CFW-8
*Honda-CFW-3
*Honda-CFW-2
*Honda-CFW-1

though they will have more information following after, I would just like to filter for anything "containing" those following texts, even though they will have more information after it. Such as Honda-CFW-6122-555.

i have included a sample workbook, if anyone could show me how to get this started i would be greatful.
 

Attachments

  • 1122.png
    1122.png
    43.9 KB · Views: 14

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
Hi and welcome to MrExcel!

Try this:

In cell N1 you must put the same header that you have in cell J3.
From cell N2 and down put the values you want to filter.
Dante Amor
ABCDEFGHIJKLMN
1COLUMN10
2Honda-CFW-6
3COLUMN1COLUMN2COLUMN3COLUMN4COLUMN5COLUMN6COLUMN7COLUMN8COLUMN9COLUMN10COLUMN11COLUMN12Honda-CFW-8
4Honda-CFW-1327Honda-CFW-3
5Honda-CFW-1333Honda-CFW-2
6Honda-CFW-1375Honda-CFW-1
7Honda-CFW-2322
8Honda-CFW-2324
13Honda-CFW-6313
14Honda-CFW-6364
15Honda-CFW-6348
16Honda-CFW-8339
17Honda-CFW-8323
18Honda-CFW-8397
24
Hoja3


Run this macro:
VBA Code:
Sub Filtering()
  Range("Tabla1[[#All],[COLUMN10]]").AdvancedFilter xlFilterInPlace, Range("N1:N6"), True
End Sub

------------
NOTE XL2BB:
For the future, it would help greatly if you could give us the sample data in a form that we can copy to test with, rather that a picture.
MrExcel has a tool called “XL2BB” that lets you post samples of your data that will allow us to copy/paste it to our Excel spreadsheets, so we can work with the same copy of data that you are. Instructions on using this tool can be found here: XL2BB Add-in
Note that there is also a "Test Here” forum on this board. This is a place where you can test using this tool (or any other posting techniques that you want to test) before trying to use those tools in your actual posts.
 
Upvote 0
Hello Dante, ty for helping.

I have copied the minisheet below, hope it helps more with this example. I have tried your code with the command button, but that did not work. Ran the code and it gave me a 400 error.

but basically, trying to filter multiple selections just using

for example Honda-CFW-6
Honda-CFW-8
Honda-CFW-3
Honda-CFW-2

and so on I Have a lot to filter for, but because it contains those criteria in the beginning it will still show up on the column.

The real numbers would contain the whole thing such as

Honda-CFW-648-133
Honda-CFW-649-134
Honda-CFW-649-136
Honda-CFW-662-137

Same for the Other numbers because they would contain

Honda-CFW-8

and so on.

Sample Excel.xlsm
ABCDEFGHIJKLMNOPQR
1Column10
2Honda-CFW-344-115
3Column1Column2Column3Column4Column5Column6Column7Column8Column9Column10Column11Column12Honda-CFW-4
4Honda-CFW-344-115Honda-CFW-8
5Honda-CFW-354-116Honda-CFW-9
6Honda-CFW-344-117
7Honda-CFW-344-118
8Honda-CFW-346-119
9Honda-CFW-346-120
10Honda-CFW-446-121
11Honda-CFW-446-122
12Honda-CFW-444-123
13Honda-CFW-444-124
14Honda-CFW-344-125
15Honda-CFW-347-126
16Honda-CFW-347-127
17Honda-CFW-547-128
18Honda-CFW-548-129
19Honda-CFW-548-130
20Honda-CFW-348-131
21Honda-CFW-348-132
22Honda-CFW-648-133
23Honda-CFW-649-134
24Honda-CFW-349-135
25Honda-CFW-649-136
26Honda-CFW-662-137
27Honda-CFW-362-138
28Honda-CFW-861-139
29Honda-CFW-864-140
30Honda-CFW-867-141
31Honda-CFW-862-142
32Honda-CFW-862-143
33Honda-CFW-362-144
34Honda-CFW-962-145
35Honda-CFW-962-146
36Honda-CFW-362-147
37Honda-CFW-362-148
38Honda-CFW-262-149
39Honda-CFW-262-150
40Honda-CFW-281-151
41Honda-CFW-382-152
42Honda-CFW-362-153
43Honda-CFW-362-154
44Honda-CFW-162-155
45Honda-CFW-179-156
46Honda-CFW-177-157
47Honda-CFW-362-158
Sheet1
 
Upvote 0
Try this:

VBA Code:
Sub Filtering()
  Range("A3:L" & Range("J" & Rows.Count).End(3).Row).AdvancedFilter _
    xlFilterInPlace, Range("N1", Range("N" & Rows.Count).End(3)), True
End Sub
 
Upvote 0
Hello Dante,

ty fo the help. it does work. is it possible to keep the headers in the colunms?
1133.png


also please help me to add your code to a command button, i have tried this but it gives me an error

VBA Code:
Private Sub CommandButton1_Click()
Sub Filtering()
  Range("A3:L" & Range("J" & Rows.Count).End(3).Row).AdvancedFilter _
    xlFilterInPlace, Range("N1", Range("N" & Rows.Count).End(3)), True
End Sub
 

Attachments

  • 1133.png
    1133.png
    16.5 KB · Views: 2
Upvote 0
sorry for posting again would anyone else be able to help me with the last issues on this code?

also anyway to get the code to sort numerically such as 1,2,3 and letters A,B,C

instead of the data showing up likke

Honda-CFW-3
Honda-CFW-6
Honda-CFW-1

need it to show up as

Honda-CFW-1
Honda-CFW-3
Honda-CFW-6

*and the ability to work with a command button?
VBA Code:
Private Sub CommandButton1_Click()
Sub Filtering()
  Range("A3:L" & Range("J" & Rows.Count).End(3).Row).AdvancedFilter _
    xlFilterInPlace, Range("N1", Range("N" & Rows.Count).End(3)), True
End Sub
 
Upvote 0
For the sort you would have to sort the whole data table not just the filtered data.

For the Command Button remove the second line "Sub Filtering()"
VBA Code:
Private Sub CommandButton1_Click()
  Range("A3:L" & Range("J" & Rows.Count).End(3).Row).AdvancedFilter _
          xlFilterInPlace, Range("N1", Range("N" & Rows.Count).End(3)), True
End Sub
 
Upvote 0
is it possible to keep the headers in the colunms?
1133.png

You can only keep one filter.
The filter I am using in the macro is advanced filter. If you want to keep the "arrow" of the autofilter. Then use the following.

Sort the data, filter, hold the autofilter arrow and it has the name of a CommandButton1_Click:

VBA Code:
Sub CommandButton1_Click()
  Dim a As Variant, b As Variant, arr() As Variant
  Dim i As Long, j As Long, k As Long, lr As Long
  
  If ActiveSheet.AutoFilterMode Then ActiveSheet.AutoFilterMode = False
  lr = Range("J" & Rows.Count).End(3).Row
  Range("A3:L" & lr).Sort Range("A4"), xlAscending, Header:=xlYes
  a = Range("A4:L" & lr).Value
  b = Range("N1", Range("N" & Rows.Count).End(3)).Value
  
  For i = 1 To UBound(a, 1)
    For j = 1 To UBound(b, 1)
      If a(i, 10) Like b(j, 1) & "*" Then
        k = k + 1
        ReDim Preserve arr(1 To k)
        arr(k) = a(i, 10)
      End If
    Next
  Next
  If k > 0 Then Range("A3:L" & lr).AutoFilter 10, Array(arr), xlFilterValues
End Sub
 
Upvote 0
Hello Dante, the sample sheet seems to work just fine. but on my other sheet the code fails to filter.

would you be able to give me some comments on what the lines do? for example this is for?


VBA Code:
Range("A3:L" & lr).Sort Range("A4"), xlAscending, Header:=xlYes

and this?
Code:
a = Range("A4:L" & lr).Value
 
Upvote 0
For it to work, the data must be in the same positions that you put in your example in post #3

If it doesn't work, you should put the data you're testing with here so I can review it.

Range("A3:L" & lr).Sort Range("A4"), xlAscending, Header:=xlYes
Sort the data

a = Range("A4:L" & lr).Value
Put all data into array 'a'
 
Upvote 0

Forum statistics

Threads
1,215,487
Messages
6,125,085
Members
449,206
Latest member
ralemanygarcia

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