Consulting

Results 1 to 15 of 15

Thread: Advanced Filter

  1. #1
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location

    Advanced Filter

    Is it possible to extract the unique records from 2 lists in Sheet1 Columns A & B and write them to those columns in Sheet2 using Advanced Filter. Or VBA if there is a simple solution. Thanks
    Peace of mind is found in some of the strangest places.

  2. #2
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    The big problem with Advanced filter is that it needs to filter to the same sheet.

    So i have filtered onto the same sheet, then moved it


    Sub FilterData()
    Dim rng As Range
    Dim sh As Worksheet
    Worksheets("Sheet1").Activate
    Set rng = Range("F1:G12")
    Set sh = ActiveSheet
    With rng
    .Offset(0, .Columns.Count).Resize(, .Columns.Count).EntireColumn.Insert
    .AdvancedFilter Action:=xlFilterCopy, _
    CopyToRange:=.Offset(0, .Columns.Count), _
    Unique:=True
    .Offset(0, .Columns.Count).Resize(, .Columns.Count).Cut _
    Destination:=Worksheets("Sheet2").Range("A1")
    .Offset(0, .Columns.Count).Resize(, .Columns.Count).Delete Shift:=xlToLeft
    End With
    End Sub
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  3. #3
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by xld
    The big problem with Advanced filter is that it needs to filter to the same sheet...
    No it doesn't.

  4. #4
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    I am confused...Which is right? Not that I am not greatful to both of you but what is correct and why?
    Peace of mind is found in some of the strangest places.

  5. #5
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Well, it's not really about right and wrong, it's about efficient and inefficient. xld's way of doing it should work - which doesn't make it wrong. But you can cut out the whole copy part which makes it slightly less efficient. Here are a couple of examples ...

    Option Explicit
    
    Sub AFoneCol()
        Dim rngFilter As Range, lngRow As Long
        lngRow = Sheets("Sheet1").Range("A65536").End(xlUp).Row
        Set rngFilter = Sheets("Sheet1").Range("A1:A" & lngRow)
        rngFilter.AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=Sheets("Sheet2").Range("A1"), _
            Unique:=True
    End Sub
    
    Sub AFtwoCols()
        Dim rngFilter As Range, lngRow As Long
        lngRow = Sheets("Sheet1").Range("A65536").End(xlUp).Row
        Set rngFilter = Sheets("Sheet1").Range("A1:B" & lngRow)
        Range("A1:B31").AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=Sheets("Sheet2").Range("A1"), _
            Unique:=True
    End Sub

  6. #6
    Distinguished Lord of VBAX VBAX Grand Master Bob Phillips's Avatar
    Joined
    Apr 2005
    Posts
    25,453
    Location
    Zack is right, it is the activesheet, not the same sheet, so my move could be avoided by working from the target sheet not the source sheet.
    ____________________________________________
    Nihil simul inventum est et perfectum

    Abusus non tollit usum

    Last night I dreamed of a small consolation enjoyed only by the blind: Nobody knows the trouble I've not seen!
    James Thurber

  7. #7
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by xld
    .. working from the target sheet not the source sheet.
    And that's the key right there. Kind of a PITA by MS imho.

  8. #8
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    I see where you are going. These examples can get me going. Thanks
    Peace of mind is found in some of the strangest places.

  9. #9
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    A question after running the second macro Zack, on the second sheet, i get the number 13. Not quite sure where that is coming from? Sure it is a simple explaination.
    Peace of mind is found in some of the strangest places.

  10. #10
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Not sure what you mean. Is that part of your data?

  11. #11
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    No. The number 13 does not appear in the data. Let me attach a sample.
    Peace of mind is found in some of the strangest places.

  12. #12
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    OOPS...
    Peace of mind is found in some of the strangest places.

  13. #13
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Can't tell ya. Works for me. Although you altered the range to something a little wierd. Go over the ranges in these examples ...

    Option Explicit
     
    Sub AFoneCol()
        Call clearMe
        Dim rngFilter As Range, lngRow As Long
        lngRow = Sheets("Sheet1").Range("A65536").End(xlUp).Row
        Set rngFilter = Sheets("Sheet1").Range("A1:A" & lngRow)
        rngFilter.AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=Sheets("Sheet2").Range("A1"), _
            Unique:=True
    End Sub
     
    Sub AFtwoCols()
        Call clearMe
        Dim rngFilter As Range, lngRow As Long
        lngRow = Sheets("Sheet1").Range("A65536").End(xlUp).Row
        Set rngFilter = Sheets("Sheet1").Range("A1:B" & lngRow)
        rngFilter.AdvancedFilter _
            Action:=xlFilterCopy, _
            CopyToRange:=Sheets("Sheet2").Range("A1"), _
            Unique:=True
    End Sub
    
    Sub clearMe()
        Sheets("Sheet2").Cells.Clear
    End Sub
    Routine clearMe is only for testing purposes.

    And btw, you should add headers/labels to your data; not sure if that is only a test file or what.

  14. #14
    Moderator VBAX Master austenr's Avatar
    Joined
    Sep 2004
    Location
    Maine
    Posts
    2,033
    Location
    ok thanks
    Peace of mind is found in some of the strangest places.

  15. #15
    VBAX Master TonyJollans's Avatar
    Joined
    May 2004
    Location
    Norfolk, England
    Posts
    2,291
    Location
    Quote Originally Posted by firefytr
    Quote Originally Posted by xld
    The big problem with Advanced filter is that it needs to filter to the same sheet.
    No it doesn't.
    Zack is right. Advanced Filter can go from any worksheet to any worksheet.


    Quote Originally Posted by xld
    Zack is right, it is the activesheet, not the same sheet, so my move could be avoided by working from the target sheet not the source sheet.
    This constraint is imposed when using the dialog but not when using code. As all the examples posted here are in code, there are no constraints.

    If you want to do it without code go to Sheet2 and invoke the dialog (Data > Filter > Advanced Filter) - select your source range on Sheet1, select your target range on Sheet2 (A1?), check the appropriate boxes/buttons and press OK.
    Enjoy,
    Tony

    ---------------------------------------------------------------
    Give a man a fish and he'll eat for a day.
    Teach him how to fish and he'll sit in a boat and drink beer all day.

    I'm (slowly) building my own site: www.WordArticles.com

Posting Permissions

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