Filter - VBA Multiple "Contains"

jamesmev

Board Regular
Joined
Apr 9, 2015
Messages
233
Office Version
  1. 365
Platform
  1. Windows
I am attempting to add a VB filter that is filtering based on the range containing a select number of options.

Example:
Column E -
ActiveSheet.Range("E:E").AutoFilter Field:=5, Criteria1:= _
Array("*" & "HLT_", "CCS_", "LCB_", "ACB_", & "*"), Operator:=xlFilterValues

If the data in E contains any of those criteria it will filter to the remaining out.

Dog
HLT_Dog
HLT_Cat
HTL_Mouse
Mouse
LCB_Mouse

I would only want the results with the prefix.

Any help would be appreciated.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
You cant do that. If you attempted to do that manually you would be restricted to two values and the same is true in code. There will be workarounds but depends what you are attempting to do.
 
Upvote 0
As @steve the fish pointed out, there are workarounds. Here's a demonstration of one way. Based on your values being in column E.

VBA Code:
Option Explicit
Sub WildCardFilter()
    Dim i As Long, ws As Worksheet, lr As Long, c As Range, arr() As Variant
    Set ws = ActiveSheet
    lr = ws.Cells(Rows.Count, 5).End(xlUp).Row
    For Each c In ws.Range("E2:E" & lr)
        If c.Value Like "HLT_*" Or _
        c.Value Like "CCS_*" Or _
        c.Value Like "LCB_*" Or _
        c.Value Like "ACB_*" Then
            ReDim Preserve arr(i)
            arr(i) = c.Value
            i = i + 1
        End If
    Next c
    With ws.Range("A1").CurrentRegion
        .AutoFilter 5, Array(arr), 7
    End With
End Sub
 
Upvote 0
Solution
Thank you this is a perfect work around!
Much Appreciated!
 
Upvote 0
As @steve the fish pointed out, there are workarounds. Here's a demonstration of one way. Based on your values being in column E.

VBA Code:
Option Explicit
Sub WildCardFilter()
    Dim i As Long, ws As Worksheet, lr As Long, c As Range, arr() As Variant
    Set ws = ActiveSheet
    lr = ws.Cells(Rows.Count, 5).End(xlUp).Row
    For Each c In ws.Range("E2:E" & lr)
        If c.Value Like "HLT_*" Or _
        c.Value Like "CCS_*" Or _
        c.Value Like "LCB_*" Or _
        c.Value Like "ACB_*" Then
            ReDim Preserve arr(i)
            arr(i) = c.Value
            i = i + 1
        End If
    Next c
    With ws.Range("A1").CurrentRegion
        .AutoFilter 5, Array(arr), 7
    End With
End Sub
I had a similar work case and this works perfectly. Registered here to Thank You!
 
Upvote 0

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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