Advance Filter Code Issue

siddo

Board Regular
Joined
May 26, 2020
Messages
106
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi, I'm not sure what is wrong with the below code but somehow I'm unable to filter the options which I want to filter. Can anyone help?
VBA Code:
Sub AdvancedFilterExample()
    'Define variables
    Dim ws As Worksheet
    Dim rng As Range
    Dim criteria As String

    'Set the worksheet you want to filter
    Set ws = ThisWorkbook.Sheets("Sheet1")

    'Set the range you want to filter
    Set rng = ws.Range("A2:AY" & ws.Cells(ws.Rows.Count, "AY").End(xlUp).Row)

    'Set the criteria for column AA
    criteria = "*10309*" & "," & "*10313*" & "," & "*10316*"

    'Set the criteria for column F
    criteria1 = "*Hitrust*" & "," & "*#$DR*" & "," & "*#$HR*" & "," & "*#$OO*"

    'Apply the advanced filter
    rng.AdvancedFilter Action:=xlFilterInPlace, Criteria1:=Array(criteria, criteria1), Operator:=xlAnd
End Sub
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Hi siddo,

maybe you mix up Advanced Filter and AutoFilter in line

VBA Code:
    rng.AdvancedFilter Action:=xlFilterInPlace, Criteria1:=Array(criteria, criteria1), Operator:=xlAnd

AFAIK code (taken from Range.AdvancedFilter method (Excel)) should look like

VBA Code:
Range("Database").AdvancedFilter _
  Action:=xlFilterInPlace, _
  CriteriaRange:=Range("Criteria")

It's stated there that if you don't have the parameter CriteriaRange set up no filter will be applied.

Holger
 
Upvote 0
Hi,

If I am not mistaken, you cannot use an Array for the Criteria.

Probably, you will need to write your Array to a range and then use the range as your Criteria
 
Upvote 0
So basically what I'm trying to do here is In column F I need the values starting from 10309 and need to exclude values which contain 10313 or 10316, Additionally after this I want to filter on column AA to get the values which contains "*Hitrust*" or "," or "*#$DR*" or"," or"*#$HR*" or," or "*#$OO*" and I'm unable to figure out how I can do this
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,885
Members
452,364
Latest member
springate

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