Advanced Filter VBA Code

bino1121

New Member
Joined
Apr 26, 2023
Messages
14
Office Version
  1. 365
  2. 2021
  3. 2019
  4. 2016
  5. 2013
  6. 2011
  7. 2010
  8. 2007
Platform
  1. Windows
Sub Advanced_Filter()

Dim Ws1 As Worksheet
Dim Ws2 As Worksheet

Dim dataSet As Long
Dim setCriteria As Long

Set Ws1 = Workbooks("Template Report.xlsx").Sheets(1)
Set Ws2 = Workbooks("Template Report.xlsx").Sheets(2)

dataSet = Ws1.Range("A" & Ws1.Rows.Count).End(xlUp).Row
setCriteria = Ws2.Range("B" & Ws2.Rows.Count).End(xlUp).Row

Range("dataSet").AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:= _
Range("setCriteria"), Unique:=False



End Sub

I am trying to advanced filter Sheet 1 from A3 to xldown (variable range) by sheet 2 from B1 xldown (Variable range) The bold section is where I get the current error Run time error 1004 Method 'Range' of object_'Global' Failed

but the goal is to filter sheet1 in place from A3 to xldown by a list on sheet2 in the same workbook starting from B1 to xldown.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
You've already defined dataSet and setCriteria as Ranges. No need for Range clarification. Try this.
VBA Code:
dataSet.AdvancedFilter (Action:= xlFilterInPlace, CriteriaRangea:= setCriteria, Unique:= False)
 
Upvote 0
You've already defined dataSet and setCriteria as Ranges. No need for Range clarification. Try this.
VBA Code:
dataSet.AdvancedFilter (Action:= xlFilterInPlace, CriteriaRangea:= setCriteria, Unique:= False)
New error trying that fix says invalid qualifier and highlights dataSet
 
Upvote 0
@bino1121, you have no ranges set.
you've declared dataSet and setCriteria as Long. They are just the last row number of those columns.
Declare them as range and try
VBA Code:
With Ws1
    Set dataSet = .Range("A3:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With

With Ws2
    Set setCriteria = .Range("B1:B" & .Range("B" & .Rows.Count).End(xlUp).Row)
End With
 
Upvote 0
@bino1121, you have no ranges set.
you've declared dataSet and setCriteria as Long. They are just the last row number of those columns.
Declare them as range and try
VBA Code:
With Ws1
    Set dataSet = .Range("A3:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With

With Ws2
    Set setCriteria = .Range("B1:B" & .Range("B" & .Rows.Count).End(xlUp).Row)
End With
You're my hero Thank you so much!!!!!!!
 
Upvote 0
@bino1121, you have no ranges set.
you've declared dataSet and setCriteria as Long. They are just the last row number of those columns.
Declare them as range and try
VBA Code:
With Ws1
    Set dataSet = .Range("A3:A" & .Range("A" & .Rows.Count).End(xlUp).Row)
End With

With Ws2
    Set setCriteria = .Range("B1:B" & .Range("B" & .Rows.Count).End(xlUp).Row)
End With
Nice catch @NoSparks . I didn't even notice that.
 
Upvote 0

Forum statistics

Threads
1,215,071
Messages
6,122,963
Members
449,094
Latest member
Anshu121

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