Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

Set 2 Filter with multiple conditions

0

Hi,

I have a set of data in Sheet1 Range(A4:G10) where in Range(A4:G4) there are the titles of my data and the filters (i.e. dropdowns).

I would like to have a vba code that sets filter in column A cell A4 to conditions equal to "pear", "apple" and "banana"and sets filter in column C cell C4 to conditions equal to dates between 01 March 2023 and 01 June 2023.

So C4 conditions are a list of dates and I would like to take all dates between 01 March 2023 and 01 June 2023 (i.e. >01 March 2023 and < 01 June 2023).

How would I do this?

Thanks,

N.

Answer
Discuss

Answers

0
Selected Answer

Hi Nick and welcome to the Forum.

You can use an array for the fruits but ">=" and "<=" for the date range.

Add this as a sheet macro ("behind" your data sheet), noting that the dates are and need to be written as US-format (mm/dd/yyyy):

Sub ACFilter()

Dim OutRng As Range

Set OutRng = Range("A4:G10")

'clear filter
OutRng.AutoFilter
' filter column A
OutRng.AutoFilter Field:=1, Criteria1:=Array("Apple", "Pear", "Orange"), Operator:=xlFilterValues
' filter column C
OutRng.AutoFilter Field:=3, Criteria1:=">=03/01/2023", Operator:=xlAnd, Criteria2:="<=06/01/2023"

End Sub

Hope this fixes things for you. If so, please remember to mark this Answer as Selected.

Discuss

Discussion

Thanks!
N.
NickRome (rep: 2) Jun 5, '23 at 2:44 pm
Glad that worked. Thanks for selecting my Answer, Nick
John_Ru (rep: 6142) Jun 5, '23 at 2:57 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login