简体   繁体   中英

Advanced Filter Excel VBA

I have created a dynamic list range for my advanced filter. I have created name called "Data". When I try to input the name into the VBA formula I receive an error. The dynamic names work for the criteria and the output range?

Range("Sheet2!Data").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range _
        ("Sheet2!Filter"), CopyToRange:=Range("Sheet2!Location"), Unique:=False

I receive the following run-time error '1004':

Method 'Range' of object'_Global' failed

If your Named Ranges' scope is for Sheet2 only, then you need to fully qualify the Range with Worksheets("Sheet2") , as in the code below:

With Worksheets("Sheet2")
    .Range("Data").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=.Range("Filter"), _
                CopyToRange:=.Range("Location"), Unique:=False
End With

If your Named Ranges' scope is Workbook then use the code below (there's no need to qualify the Range with the Worksheet ):

Range("Data").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Range("Filter"), _
                CopyToRange:=Range("Location"), Unique:=False

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM