Add another condition to Filter code

Nick70

Active Member
Joined
Aug 20, 2013
Messages
299
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a code for a filter see below:
VBA Code:
Sub ApplyFilter()
 
    Dim filterRange As Range
    Dim filterConditions As Range

    'Set the filter range and conditions range
    Set filterRange = Sheets("Sheet2").Range("A4:G20")
    Set filterConditions = Sheets("Sheet1").Range("A5:A" & Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row)

    'Apply the filter based on the conditions in Sheet1
    filterRange.AutoFilter Field:=4, Criteria1:=Application.Transpose(filterConditions), Operator:=xlFilterValues

End Sub

I would like to change filterConditions so that is also looks at data in column B.
So code would change to something like

VBA Code:
    Set filterConditions = Sheets("Sheet1").Range("A5:A" & Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row) &  Sheets("Sheet1").Range("B5:B" & Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row)[/B]

But this does not work.

Can someone please let me know how I can add additional condition
VBA Code:
Set filterConditions = Sheets("Sheet1").Range("B5:B" & Sheets("Sheet1").Cells(Rows.Count, "B").End(xlUp).Row)
?

Thanks,
N.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)
 
Upvote 0
Hello I use Excel version 2208 I will update my Account details
 
Upvote 0
I use office version 365 I have added this to my Account details. I hope someone will still answer my question as it shows it has some replies to it although they are not the answer.
Thanks,
N.
 
Upvote 0
I hope someone will still answer my question
Give me a chance. :(

How about
VBA Code:
Sub ApplyFilter()
 
    Dim filterRange As Range
    Dim filterConditions As Variant

    'Set the filter range and conditions range
    Set filterRange = Sheets("Sheet2").Range("A4:G20")
    filterConditions = Evaluate("tocol(" & Sheets("Sheet1").Range("A5:B" & Sheets("Sheet1").Cells(Rows.Count, "A").End(xlUp).Row).Address & ",1)")

    'Apply the filter based on the conditions in Sheet1
    filterRange.AutoFilter Field:=4, Criteria1:=Application.Transpose(filterConditions), Operator:=xlFilterValues

End Sub
 
Upvote 0
A better version
VBA Code:
Sub ApplyFilter()
 
    Dim filterRange As Range
    Dim filterConditions As Variant

    'Set the filter range and conditions range
    Set filterRange = Sheets("Sheet2").Range("A4:G20")
    With Sheets("Sheet1")
        filterConditions = .Evaluate("torow(" & .Range("A5:B" & .Cells(Rows.Count, "A").End(xlUp).Row).Address & ",1)")
    End With
    'Apply the filter based on the conditions in Sheet1
    filterRange.AutoFilter Field:=4, Criteria1:=filterConditions, Operator:=xlFilterValues

End Sub
 
Upvote 0
It works perfectly!

Thanks a apologies for doubting you would give answer! :)(y)
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
A better version
VBA Code:
Sub ApplyFilter()
 
    Dim filterRange As Range
    Dim filterConditions As Variant

    'Set the filter range and conditions range
    Set filterRange = Sheets("Sheet2").Range("A4:G20")
    With Sheets("Sheet1")
        filterConditions = .Evaluate("torow(" & .Range("A5:B" & .Cells(Rows.Count, "A").End(xlUp).Row).Address & ",1)")
    End With
    'Apply the filter based on the conditions in Sheet1
    filterRange.AutoFilter Field:=4, Criteria1:=filterConditions, Operator:=xlFilterValues

End Sub
Hello Fluff,

As if life was not difficult enough...

In reality my two ranges in the two columns are different (column B has more data) so we should take them separately and add them together.
Current code takes last row from column A but in reality column B has more data so we will miss some data from column B.

Do you know how to amend code?

Thanks,
N.
 
Upvote 0
How about
VBA Code:
Sub ApplyFilter()
 
    Dim filterRange As Range
    Dim filterConditions As Variant
    Dim Lr As Long
    
    'Set the filter range and conditions range
    Set filterRange = Sheets("Sheet2").Range("A4:G20")
    With Sheets("Sheet1")
        Lr = .Range("A:B").Find("*", , , , , xlPrevious, , , False).Row
        filterConditions = .Evaluate("torow(" & .Range("A5:B" & Lr).Address & ",1)")
    End With
    'Apply the filter based on the conditions in Sheet1
    filterRange.AutoFilter Field:=4, Criteria1:=filterConditions, Operator:=xlFilterValues

End Sub
 
Upvote 0
Solution

Forum statistics

Threads
1,215,079
Messages
6,123,005
Members
449,092
Latest member
masterms

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