View Single Post
 
Old 07-14-2022, 04:21 PM
p45cal's Avatar
p45cal p45cal is offline Windows 10 Office 2019
Expert
 
Join Date: Apr 2014
Posts: 871
p45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond reputep45cal has a reputation beyond repute
Default

I couldn't find a way of doing this with an array of more than 2 elements while using AND logic, so a convoluted way:
Code:
Sub blah()
crits = Array("BYCYCLE", "MOTOR", "12V")
'Is there an autofilter on the sheet already?:
On Error Resume Next
Set myAutoF = ActiveSheet.AutoFilter
On Error GoTo 0
    
If myAutoF Is Nothing Then Range("A1:B1").AutoFilter    'if there isn't, add one else use existing one. This behaviour can be tweaked.
'get the range of column 2 databody:
Set myrng = ActiveSheet.AutoFilter.Range.Columns(2)    '2nd column
Set myrng = Intersect(myrng, myrng.Offset(1))    'data body only
Set mydic = CreateObject("scripting.dictionary")    'dictionary to hold full strings of those conforming to criteria.
For Each cll In myrng.Cells    'go through each cell (if range is huge we can speed this up by doing it in-memory)
  found = True
  For Each sstr In crits
    If Not cll.Value Like "*" & sstr & "*" Then    'if any string NOT found then stop the loop and move to next cell
      found = False
      Exit For
    End If
  Next sstr
  If found Then mydic(cll.Value) = vbNullString    'if it's passed all the tests then add full cell content to dictionary
Next cll
'If at least one found then filter for it else don't filter:
If mydic.Count > 0 Then ActiveSheet.AutoFilter.Range.AutoFilter Field:=2, Criteria1:=mydic.keys, Operator:=7
End Sub
Reply With Quote