Advanced filtering using VBA - dynamic criteria

Steeviee

Active Member
Joined
Sep 9, 2009
Messages
380
Dear all,

I am using some code to do advanced filtering. When a drop down box is used, the resultant choice populates the criteria fields (G2) and advanced filtering is used.

What I want to do is have 4 choices. I then need the criteria range to be rows 1:5. However, if say, the 4th choice is All (i.e. no choice) the criteria range should be rows 1:4.

I have tried assigning a variable b:
Code:
If Sheets(3).Range("ven4sel").Value = "All" Then
b = 4
    If Sheets(3).Range("ven3sel").Value = "All" Then
    b = 3
        If Sheets(3).Range("ven2sel").Value = "All" Then
        b = 2
        Else
        b = 3
        End If
    Else
    b = 4
    End If
Else
b = 5
End If
    Range("A12:AU10000").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
        Rows("1:b"), Unique:=False

but it really doesn't like this.

Does anyone have any suggestions of how I could do this, depending on the number of choices made?

Many thanks in advance.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Forum statistics

Threads
1,224,551
Messages
6,179,473
Members
452,915
Latest member
hannnahheileen

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