'Include a partial string match in an array
I'm trying to edit my VBA code to filter for multiple values (which already exist in my worksheet) and for partial matches to also be filtered. This is my current code:
ActiveSheet.Range("$A$6:$AB$9903").AutoFilter Field:=15, Criteria1:=Array( _
"Desktop", "Monitor", "Non-Standard Desktop", _
"Non-Standard Notebook", "Notebook"), Operator:=xlFilterValues
I would like to add in another criteria, like anything that contains the word "Headset". I tried adding in * Headset* to the array above but it did not work. I also tried creating a new chunk of code for just words containing 'Headset', and while it did filter for those cells, it only filtered for those.
I'm not really sure what other options I have. I checked other people's solutions but it looks like in their solutions, they're doing what I did above (filtering only for words containing that specific string). Any advice? Thanks!
Solution 1:[1]
Using AdvancedFilter When Many Criteria With Wild Characters
You can use the following...
With ActiveSheet
If .FilterMode Then .ShowAllData
With .Range("A6:AB9903") ' with headers
Dim DataRange As Range ' without headers
Set DataRange = .Resize(.Rows.Count - 1).Offset(1)
.AdvancedFilter xlFilterInPlace, .Range("AD6").CurrentRegion
End With
Dim VisibleDataRange As Range ' without headers
On Error Resume Next
Set VisibleDataRange = DataRange.SpecialCells(xlCellTypeVisible)
On Error GoTo 0
If Not VisibleDataRange Is Nothing Then
' Do what you need to do (copy, delete...), e.g.:
Debug.Print VisibleDataRange.Address(0, 0)
'Else ' no filtered data found
End If
' Remove the filter.
.ShowAllData
End With
... if you use the range AD6:AD12 filled with the following:
COL15
=Non-Standard Notebook
=Monitor
=Non-Standard Desktop
=Notebook
=Desktop
*headset*
... where you will replace COL15 with the header in O6. You will enter the values e.g. like this: ="=Monitor" or '=Monitor.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
