简体   繁体   中英

VBA: autofilter a range and copy to existing sheet

Sorry to ask for help but i've been trying lots of macros and none did the job right. I don't know where to look for the solution.

  • I have a sheet named "ABC" where I have data inputed by users into a range A:F (starting from A2 to go under the heathers & could be up to 20.000 rows).
  • On column GI have the criteria (Yes/No)

So I want to copy (paste values & keep formating) all the rows to A:F where G="Yes" to another sheet named "ANAF ANG" into a specified range (same A:F, starting A2).

note: the initial sheet from where the data is copied must clear filters after its done.

How.. ?

you can use this commented code:

Sub main()
    With Sheets("ABC") '<--| reference your sheet
        With .Range("G1", .Cells(.Rows.Count, 1).End(xlUp)) '<--| reference its column "A:G" range from row 1 down to column "A" last not empty row
            .AutoFilter Field:=7, Criteria1:="yes" ''<--| filter referenced range on its 7th column (i.e. column "G") with "yes" values
            If Application.WorksheetFunction.Subtotal(103, .Resize(, 1)) > 1 Then .Resize(.Rows.Count - 1, 6).Offset(1).SpecialCells(xlCellTypeVisible).Copy Destination:=Worksheets("ANAF ANG").Range("A2")
        End With
        .AutoFilterMode = False
    End With
End Sub

Don't try to write the VBA script from scratch, use Excel macro recording. If you know how to achieve your result interactively, the necessary code is created for you. If you are still unsatisfied with the code you can tune it, then - much easier than reading the reference manual for hours, just to get you started.

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