+ Reply to Thread
Results 1 to 7 of 7

Copy filtered range

Hybrid View

  1. #1
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,062

    Copy filtered range

    I have a routine that copies the visible cells in an autofilter range to another sheet, based on the criteria of how much data is revealed by the autofilter. I use this routine multiple times, and it works without a hitch, except in this instance. In this instance, it copies all of the available data, not only the visible cells. I've been trying to find out what the problem is, to no avail, and so am asking for help.

    The code is below. The macro first filters column P, and pulls up anything that's not "UNAU". Then it filters column M for anything other than "G" or "E". The expected result is that nothing will be found, and that's what happens in this case. So, my autofilter is only displaying the headings. There's a test (Lcount) to see if the # of visible cells (including headers) is less than or greater than 20. If less, nothing is copied. If more, all of the visible cells, except for the headers, are supposed to be copied and pasted into my "Results" worksheet. But for some reason, even though the autofilter displays no results, my "Results" page is filled with ALL of the data that the autofilter is filtering on.

    Help! It's driving me crazy!

    Sub Test()
    Dim DstWkb As Workbook
      Dim rng As Range
      Dim RngEnd As Range
      Dim cell As Range
      Dim NumRows As Variant
      Dim lcount As Variant
      Dim rTable As Range
      Set rng = Selection
      
    Set DstWkb = Workbooks("Active Position Checklist.xls")
                         
    
          DstWkb.Worksheets("APRData").Select
            With DstWkb.Worksheets("APRData")
                    
             'Restrain the filter to cells from A1 to the last entry in column Q
                Set rng = .Range("A1:Q1")
                Set RngEnd = .Cells(Rows.Count, rng.Column).End(xlUp)
                Set rng = IIf(RngEnd.Row < rng.Row, rng, .Range(rng, RngEnd))
    
    '3)    Test to see if UNAU doesn't have a G or E
        rng.EntireRow.AutoFilter Field:=16, Criteria1:="=UNAU", Operator:=xlAnd
        rng.EntireRow.AutoFilter Field:=13, Criteria1:="<>G", Operator:=xlAnd, _
            Criteria2:="<>E"
        
        'See whether there's data or not
            lcount = .AutoFilter.Range.Rows.SpecialCells(xlCellTypeVisible).Count
                    
            If lcount > 20 Then
            
            'Copy and paste only the filtered data
                Set rTable = Sheets("APRData").AutoFilter.Range
                Set rTable = rTable.Resize(rTable.Rows.Count - 1)
            'Move new range down to start at the first data row.
                Set rTable = rTable.Offset(1)
                rTable.Copy Destination:=DstWkb.Worksheets("Results").Range("A65536").End(xlUp).Offset(1, 0)
             End If
    End With
    
    End Sub
    Last edited by jomili; 07-16-2010 at 08:48 AM.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy filtered range

    This is the approach I use, not as elegant as yours but reliable for me:

    lcount = .Cells(.Rows.Count, 13).End(xlUp).Row
    
    If lcount > 1 Then .Range("A2:A" & lcount).EntireRow.Copy _
        DstWkb.Sheets("Results").Range("A" & Rows.Count).End(xlUp).Offset(1)
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,062

    Re: Copy filtered range

    Thanks for the help. I haven't tried it yet (BUSY DAY!), but before I do I'd like to understand it more. What does the "13" do in the "Cells(Rows.Count" section? Is that relating to column M? So then the "IF LCount > 1" would check to see if there's only 1 entry in M, and if so it wouldn't copy, but if there's more than 1, it would copy?

    Thanks for your help, and thanks in advance for your explanation.

  4. #4
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Copy filtered range

    Quote Originally Posted by jomili View Post
    Thanks for the help. I haven't tried it yet (BUSY DAY!), but before I do I'd like to understand it more. What does the "13" do in the "Cells(Rows.Count" section? Is that relating to column M? So then the "IF LCount > 1" would check to see if there's only 1 entry in M, and if so it wouldn't copy, but if there's more than 1, it would copy?

    Thanks for your help, and thanks in advance for your explanation.
    Yes, the 13 is for column M, the last column you Autofiltered, which is why I used it. You could probably use any column in the dataset...

    Since your headers/titles are in row1, if you filter two columns and ALL rows end up hidden, then the .End(xlUp) method I used on column M jumping up from the bottom of the column will fly all the way up to row 1. So if lcount = 1 then we know no other rows are visible, so we skip copying.

    If lcount > 1 then we know some rows are visible, so we just copy from row 2 down to lcount...Autofiltered data will only copy visible rows in this instance so you're good to go.

  5. #5
    Valued Forum Contributor
    Join Date
    12-02-2009
    Location
    Austin, Tx
    MS-Off Ver
    Office 365 64-Bit, 2108, build 14326.21018
    Posts
    4,062

    Re: Copy filtered range

    Wow, I don't think I've ever recieved so much great teaching and help from one of my posts.

    JBeaucaire, thank you so much for the alternative routine and the explanation of how it works.

    SNB, thank you so much for another alternative routine. There's more ways than one to skin a cat, eh?

    Leith Ross, thank you so much for pointing out what was going wrong with my original method. I don't like making errors, but I count it a plus when I get to learn soemthing from making mistakes.

    I'm going to mark this one solved. God bless you all.

  6. #6
    Forum Expert snb's Avatar
    Join Date
    05-09-2010
    Location
    VBA
    MS-Off Ver
    Redhat
    Posts
    5,649

    Re: Copy filtered range

    This works for me:

    Sub snb()
        With Thisworkbook.sheets("APRData").UsedRange.Resize(, 17)
           .AutoFilter 16, "UNAU"
           .AutoFilter 13, "<>G", xlAnd, "<>E"
           If .Columns(2).SpecialCells(xlCellTypeVisible).Count > 1 Then .Offset(1).Copy Sheets("Results").Range("A65536").End(xlUp).Offset(1)
            .AutoFilter
        End With
    End Sub

  7. #7
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: Copy filtered range

    Hello ,

    Your problem may lie with the SpecialCells method. All data will be copied, or deleted whenever there are more than 8192 individual range areas. An area is a group of separate cells. For example, Range("A1:C10, F5:10") represents 2 areas.

    Your statement
        'See whether there's data or not
            lcount = .AutoFilter.Range.Rows.SpecialCells(xlCellTypeVisible).Count

    is actually returning the cell count in column "A:Q" that have been filtered and not the row count. To return the row count of the filtered cells, the Range property needs to follow SpecialCells.
        'See whether there's data or not
            lcount = .AutoFilter.Range.SpecialCells(xlCellTypeVisible).Rows.Count
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1