Great Difficulty Getting Results From VBA Advanced Filter

Ark68

Well-known Member
Joined
Mar 23, 2004
Messages
4,564
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
I am simply trying to filter data according to my advanced criteria range in worksheet "ws_fac" (in 2nd workbook) and copy it over to a range starting at AN3 in worksheet "ws_lists" (of 1st primary code holding workbook)

Here is the code I'm struggling with:
Rich (BB code):
If ws_fac.FilterMode Then ws_fac.ShowAllData                                              'turn off any filtering if data is filtered
ws_lists.Range("AN3").CurrentRegion.Offset(1).ClearContents                      'clear the destination range (filtered data to be copied here)
Set rgData = ws_fac.Range("A1").CurrentRegion                                           'define the source range (all data in ws_fac including header row 1)
Set rgCriteriaRange = ws_lists.Range("AI2:AJ3").CurrentRegion                   'criteria range (AI3 AND AJ3)
Set rgCopyToRange = ws_lists.Range("AN3").CurrentRegion.Rows(1)          'destination target (ws_lists starting at AN3)
rgData.AdvancedFilter xlFilterCopy, rgCriteriaRange, rgCopyToRange        'apply filter
lrfaclst = ws_lists.Cells(ws_lists.Rows.Count, "AN").End(xlUp).Row                'get the value of the last row of copied data range
ws_lists.Range("AN" & lrfaclst + 1) = "Nothing suitable."                             'add a value to the end of the column of data


The line in red is giving me a "The extract range has a missing or invalid field name." I'm unsure why. The column headers match the reference headers in my data. All the columns in my source data have a header label, and no column is without a header label.

Here is the structure of my source data worksheet:
Facilities.xlsx
ABCDEFGHIJKLMNOPQR
1hh FIELD (do not alter)AN RESOURCE TYPECLASSRESPTYPEUNITLITLABELWashroomWR_CodeFAMILYWP_SideGROOMACT-PASSCONFIGGOALSPARKAddress
2hhhhhhhhhhhhhhhhhhhhhhhhBBPhhhh1NhhhhhhhhhhhhhhhhhNxBPCRPAhhhh
3lllllllllllllllllABPllll1YlllllllllllllllYxBPCRPAlllll
4mmmmmmmmmmmmmmmmBBPField1NmmmmmmmmmNxBPnaANCNAmmm
5mmmmmmmmmmmmmmmmABPField2YmmmmmmmmmNxBPnaAS1 90m x 61.5mNAmmm
Facilities


Here is my criteria range:
SignatureSheets.xlsm
AIAJ
1Advanced Filter1
2ACT-PASSLABEL
3PWP*
lists


And finally, the structure of my destination:
SignatureSheets.xlsm
ANAO
1FacList
2ACT-PASSLABEL
3
4
5
6
lists


I don't yet quite understand VBA based advanced filtering so I'm struggling to figure out where I need to make corrections. Be very grateful for any one that can offer up some advice, solutions.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
I don't believe the Advanced Filtering part is what is causing you the issue but rather your use of Current Region for the Criteria and Destination.
Can you clarify that you are only expecting to output 2 columns ?
 
Upvote 0
Hi Alex, thank you so much for considering my issue. You are likely very correct as I have no idea what the CurrentRegion arguments are meant to do. I have been self teaching with tutorials and in the instance I was following they were used.

Yes, if it is possible, to only copy the ACT-PASS and LABEL columns of the filtered data (from AND results of the ACT-PASS and LABEL criteria).
 
Upvote 0
Upvote 0
In terms of the code, see if this helps.

VBA Code:
Sub TestAdvancedFilter_modv02()

    Dim ws_fac As Worksheet, ws_lists As Worksheet
    Dim rgData As Range, rgCriteriaRange As Range, rgCopyToRange As Range
    Dim lrfaclst As Long
    
    Set ws_fac = Worksheets("Facilities")
    Set ws_lists = Worksheets("lists")    

    If ws_fac.FilterMode Then ws_fac.ShowAllData                                    'turn off any filtering if data is filtered
    ws_lists.Range("AN3").CurrentRegion.Offset(2).ClearContents                     'clear the destination range (filtered data to be copied here)
    
    Set rgData = ws_fac.Range("A1").CurrentRegion                                   'define the source range (all data in ws_fac including header row 1)
    Set rgCriteriaRange = ws_lists.Range("AI2:AJ3")                                 'criteria range (AI3 AND AJ3)
    Set rgCopyToRange = ws_lists.Range("AN2").CurrentRegion.Rows(2)                 'destination target (ws_lists starting at AN3)
    
    rgData.AdvancedFilter xlFilterCopy, rgCriteriaRange, rgCopyToRange              'apply filter
    lrfaclst = ws_lists.Cells(ws_lists.Rows.Count, "AN").End(xlUp).Row              'get the value of the last row of copied data range
    If lrfaclst = 2 Then ws_lists.Range("AN" & lrfaclst + 1) = "Nothing suitable."  'add a value to the end of the column of data

End Sub
 
Upvote 0
Solution
See if this helps:

1657461784961.png
 
Upvote 0
Hi Alex,
I really appreciate the effort put into helping me with this. CurrentRegion seems pretty straight forward. I've been defining ranges in a very inefficient way knowing now how CurrentRegion can help do the same thing. But I also feel it could cause problems if not fully understood.
With the illustration you provided in posts 6, I know understand better where your code corrections make sense,so I thank you for providing both. Things now work as needed. Thank you.

I am curious about the Offset(2) reference in the ClearContents line. I've been playing around with offset, but still not comfortable enough to use it for it's full power. Not sure yet if I undersstand how the offset affected the CurrentRegion. I think it was needed to avoid clearing the contents of AN1:AO2. If I understand CurrentRegion correctly, that if I copied my data to AN4 instead of AN3 (therfore leaving cells AN3:AO3 blank) that this code would do the same:
Code:
ws_lists.Range("AN4").CurrentRegion.ClearContents

Which brings me to the rgCopyToRange statement where rows(2) was added. I assume isolates the needed data by pointing it to the second row of the "CurrentRegion" (which consists of 2 rows and 2 columns). Would not :
Code:
Set rgCopyToRange = ws_lists.Range("AN2:AN3")
not accomplish the same?
 
Upvote 0
Have you checked all the headers for trailing spaces?
Hi Mike ... yes, that was the first thing I checked as it made sense to believe something wasn't right with the headers. The spelling was all correct, so I used LEN to determine if the header values had spaces I couldn't see. They all revealed that the values were just as they appeared.
 
Upvote 0
Using the image below:
Ideally you don't want to have an additional heading row "up against the table" you are working with.
So you want to leave a blank row between Advanced Filter1 in AI and the data under it
and also for FacList in AN and the data under it.
If you do that then CurrentRegion based on ACT-PASS in either AI and AN would not pick up the additional and irrelevant row.

Excel's Filter, Sort, Create Pivot & Convert to table functions also use current region, so having the blank row separating what should be the column headings from data above it helps with those function as well.

The VBA offset function has 2 parameters
.Offset(ByNoOfRows, ByNoOfColumns).
If you only want to Offset by rows you can leave out the latter and just put
.Offset(ByNoOfRows)

Offset by rows moves the whole range area you are working with down by that no of rows.
So Range("A10:E13").Offset(5) becomes Range("A15:E18").
CurrentRegion is just a range and works the same way.
Rows and Columns and Cells are determined at the sheet level if you don't qualify it using a range but if you use a range then it is range dependant.
  • Rows(2).Address gives $2:$2 (Row 2 of the Sheet)
    but
  • Range("A10:E13").Rows(2).Address gives $A$11:$E$11 (Row 2 of the Range("A10:E13"))

1657465014161.png
 
Last edited:
Upvote 0

Forum statistics

Threads
1,214,851
Messages
6,121,931
Members
449,056
Latest member
denissimo

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