AdvancedFilter Works day1, doesn't work day2?

jvanbonn

Board Regular
Joined
Mar 11, 2011
Messages
71
Hi everyone,

Thanks for you wisdom and knowledge.

I have VBA code to create a unique list of names from a repetitive list of names. The list of names is 32 long including the header row, and has 5 unique names.

My goal is to create a unique list of names on Worksheets("Seniors List"). The code worked for me yesterday, but it is not working today. Any ideas why this would occur? How can I get this to work consistently?

Code:
Dim w As Worksheet, s As Worksheet
Dim lastSeniorRow As Long
  
  Set w = Worksheets("ACE Locations")
  Set s = Worksheets("Seniors List")
  
  s.Range("A:A").Clear  'Clear the existing list
  
  lastSeniorRow = w.Cells(Rows.Count, 9).End(xlUp).Row  ' =32
    
  w.Range(w.Cells(1, 9), w.Cells(lastSeniorRow, 9)).AdvancedFilter _
    Action:=xlFilterCopy, _
    CopyToRange:=w.Columns("A:A"), Unique:=True
Thanks in advance!
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
Well, I figured out what is going on, but I have know idea why.

When the VBA code executes the AdvancedFilter, there appears to be a residual CriteriaRange:= from a previous filter, which of course is never true, thus no results but the header.

My fix was to add CriteriaRange:="", _ to the code to force no filter criteria.:

Code:
Dim w As Worksheet, s As Worksheet
Dim lastSeniorRow As Long
  
  Set w = Worksheets("ACE Locations")
  Set s = Worksheets("Seniors List")
  
  s.Range("A:A").Clear  'Clear the existing list
  
  lastSeniorRow = w.Cells(Rows.Count, 9).End(xlUp).Row  ' =32
    
  w.Range(w.Cells(1, 9), w.Cells(lastSeniorRow, 9)).AdvancedFilter _
    Action:=xlFilterCopy, _
    [B][COLOR=Red]CriteriaRange:="", _[/COLOR][/B]
    CopyToRange:=w.Columns("A:A"), Unique:=True
Why would Excel populate the CriteriaRange option when non was specified?
 
Upvote 0
yes residual criteria range remains. even if you do not give the criteria range it takes the previous range as valid.
 
Upvote 0

Forum statistics

Threads
1,224,537
Messages
6,179,405
Members
452,911
Latest member
a_barila

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