简体   繁体   中英

VBA - copy filtered range without select

I have a range of data and I want to filter by specific criteria and then copy the filtered data to another sheet.

This is my code:

           'filter and Lastrow are variables previously declared    
            Sheet1.Activate
            Sheet1.Range("$A$1:$AW$" & Lastrow).AutoFilter field:=1, Criteria1:=filter
            Sheet1.Range("A1").Select

            Range(Selection, Selection.End(xlDown)).Select
            Range(Selection, Selection.End(xlToRight)).Select
            Selection.Copy Destination:=Sheet2.Range("A1")

The problem is that this requires selecting and selecting requires the relevant sheet to be active. Is there a way to do it without first selecting (Sheet1.Range("A1").select)? Note, that my filtered range is not a table.

Thanks!

You already have your lastrow set in a variable. Try,

with worksheets("sheet1").Range("A1:AW" & Lastrow)
    .AutoFilter field:=1, Criteria1:=filter
    .Copy Destination:=Sheet2.Range("A1")
end with

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