Consulting

Results 1 to 6 of 6

Thread: AdvancedFilter result range EXCLUDING input column title

  1. #1
    VBAX Newbie
    Joined
    Jul 2017
    Posts
    2
    Location

    AdvancedFilter result range EXCLUDING input column title

    Hi Everyone,

    I am new to the forum - beginner to intermediate level VBA user and advanced Excel user - looking forward to helping and being helped on this forum =)

    My first question is this - I am trying to use AdvancedFilter function to parse a range of cells from one sheet into another (via VBA), however, I need to exclude the title of the input range from my results...

    VB:
    Worksheets("Worksheet1").Range("J2" & ":J" & LastrowJ).AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=Sheets("Worksheet2").Range("D1819"), CopyToRange:=Worksheets("Worksheet3").Range("A" & Lastrowa), Unique:=True


    Appendix: J2 is the header/title of my input range and LastrowJ is the value of the last cell; D18 : D19 contains filtering criteria; Lastrowa is the value of the first cell to where my data is copied/filtered through.

    I could simply refer my input range to start one cell AFTER the title, however, that will disallow my CriteriaRange to be applied correctly during filtering (e.g. CriteriaRange contains information on the column header used for filtering).

    Alternatively, I could point my criteria to the first cell in the input range, instead of the title, but that is least desirable...

    If anyone has any ideas/suggestions - please share - I will be very much obliged!

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    AdvancedFilter will move the headers to the CopyToRange.
    But, once it has done that, that row of the output can be blanked or deleted.

  3. #3
    VBAX Sage
    Joined
    Apr 2007
    Location
    United States
    Posts
    8,726
    Location
    In Excel 2016, doing the operation manually I get an error that says "You can only copy filtered data to the active sheet"
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

  4. #4
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Sub Test()
        
        Dim Data As Range, crit As Range, Tgt As Range
        
        Set Data = Sheet1.Range("A7").CurrentRegion
        Set crit = Sheet1.Range("A1").CurrentRegion
        Set Tgt = Sheet2.Range("A1")
        
        Data.AdvancedFilter Action:=xlFilterInPlace, CriteriaRange:=crit
        Data.Offset(1).SpecialCells(12).Copy Tgt
        Data.Parent.ShowAllData
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Newbie
    Joined
    Jul 2017
    Posts
    2
    Location
    Quote Originally Posted by mikerickson View Post
    AdvancedFilter will move the headers to the CopyToRange.
    But, once it has done that, that row of the output can be blanked or deleted.
    Whenever I run the following delete macro, it works in the VBA step/break mode, however, once I run it through the macro button, it doesn't work (perhaps advanced filter runs again albeit being before in the code and overwrites it):

    Dim Deleterow as Long
    Deleterow = Application.WorksheetFunction.Match("Mycase", Worksheets("Sheet3").Range("A:A"), 0)
    Rows([Deleterow]).EntireRow.Delete
    So what happens, is when I run my module via VBA, everything takes place as expected. If, I run it through the button that is designed to perform this task, the deletion doesn't happen (or happens and is overwritten perhaps - no way of testing)...

    Does anyone have any ideas?

    Thank you kindly!

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Should Mycase be string as shown or should it be a variable. If that is correct try
    Dim Deleterow As Long
    On Error Resume Next
    Deleterow = Application.WorksheetFunction.Match("Mycase", Worksheets("Sheet3").Range("A:A"), 0)
    If Deleterow <> 0 Then Rows([Deleterow]).EntireRow.Delete
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

Posting Permissions

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