VBA Code To Filter By Wildcard String Pattern

In this post we will see how to filter a column by applying wildcard text pattern criteria.

Suppose we have a range in excel with values in it and we would like to filter it by two or more wildcard text pattern, we can achieve the filtering result by using “*” as wildcard operator in VBA code.

Below is the data which shows the range of values in which we want to apply the wildcard filtering criteria, we want to filter all the rows that has “Mark” or “David” in it i.e. row number 2,4 and 6.




Excel showing values to be filtered after applying wildcard pattern matching

Step 1

Open VB editor from Developer tab in excel as shown in the figure.

Open developer tab to write VBA to filter by wild card text pattern

Step 2

Insert a module in which we will write a VBA code to filter the data by wildcard text pattern.

Insert a module in the VB editor

Step 3

Now double click the newly created module to open that and paste the below code in the module.

Sub Filetr_Widlcard()

With ActiveSheet.Cells(1, 1).CurrentRegion

    .AutoFilter Field:=1, Criteria1:=”=*Mark*”, Operator:=xlOr, Criteria2:=”=*David*”

End With

End Sub




VBA code to apply filter on wildcard condition

This vba code is specifying two wildcard criteria i.e. “*Mark*” and “*David*” which basically tells the VBA to look for rows that has “Mark” or “David” in it, * denotes 0 or more characters.

Now run the code by pressing F5 or by pressing play button the editor.

Below is the result that we get after running the code, as you can see in the result two wildcard text pattern filter is applied.

Result showing the data in column A is filtered by wildcard pattern matching

Hope this helped.




Share The Knowledge

Random Posts

  • Free Download Excel GST Invoice

    Below is the link to download Excel GST Invoice Template. What are the specific mandatory fields in an GST compliant […]

    Share The Knowledge
  • Assign Random Characters to Cells In Excel

    In this pose we will see how could we generate random characters in excel and assign random characters to cells […]

    Share The Knowledge
  • Unit Conversion In Excel

    Let’s see how to do unit conversion in excel. We will see how we could leverage a simple convert function […]

    Share The Knowledge
  • Sum Numbers Separated By Symbol In Excel

    sum numerical values separated by symbol in excel, excel formula to sum numerical values separated by symbol in excel, excel formula to remove symbol and calculate sum, sum numerical values separated by @ in excel,
    sum numerical values separated by comma in excel

    Share The Knowledge