+ Reply to Thread
Results 1 to 16 of 16

Advanced Filter does not work in a recorded Macro

Hybrid View

  1. #1
    Registered User
    Join Date
    03-06-2008
    Location
    Ireland
    Posts
    26

    Angry Advanced Filter does not work in a recorded Macro

    I recorded a macro to use Advanced Filtering but the Macro will not work.

    When I recorded it, there were no issues and every time I run it manually it works.

    There are no dates involved.

    I am clearing any current data on the OutputSheet and then I am selecting a list of items on one worksheet and filtering out any duplicates so I am left with a unique list on the OutputSheet.

    I position the cursor in call "A1" on "OutputSheet".

    Code is below:

    Sub CopyData
    
    Sheets("OutputSheet").Range("A1:A100").Select
    Selection.ClearContents
    Sheets("OutputSheet").Range("A1").Select
    Sheets("InputSheet").Range("F3:F100").AdvancedFilter Action=XlFilterCopy _
           , CopyToRange:=Sheets("OutputSheet").Range("A1"), Unique=True
    
    End Sub
    The input range includes a header (in F3).
    The output position A1 is the header.
    Last edited by jnmturner; 01-09-2014 at 04:16 AM. Reason: Add Code Tags

  2. #2
    Registered User
    Join Date
    03-06-2008
    Location
    Ireland
    Posts
    26

    Re: Advanced Filter does not work in a recorded Macro

    I am using Excel 2010

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    How does it not work?

    PS Can you use code tags?
    If posting code please use code tags, see here.

  4. #4
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Advanced Filter does not work in a recorded Macro

    When I try to output an advanced filter to another sheet, I get "You can only copy filtered data to the active sheet". If I change it to the current sheet, it works fine, as does the macro. Try this:
    Sub Macro1()
    Dim EndRw As Long
    Const TempCol = 5 ' Replace with an open column number
    
        Columns(TempCol).ClearContents
        EndRw = Range("A" & Rows.Count).End(xlUp).Row
        Range("A1:A" & EndRw).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Cells(1, TempCol), Unique:=True
        Columns(TempCol).Cut Destination:=Sheets("OutputSheet").Columns(1)
    End Sub

  5. #5
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643
    natefarm

    When using code for advanced filter there's no restriction on where you can copy to.

  6. #6
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Advanced Filter does not work in a recorded Macro

    Norie is correct! The following code works for me.
    Sub Macro1()
        Sheets("OutputSheet").Columns(1).ClearContents
        EndRw = Range("A" & Rows.Count).End(xlUp).Row
        Range("F1:F" & EndRw).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("OutputSheet").Range("A1"), Unique:=True
    End Sub
    Last edited by natefarm; 01-08-2014 at 01:48 PM. Reason: Changed col from A to F.

  7. #7
    Forum Guru Izandol's Avatar
    Join Date
    03-29-2012
    Location
    *
    MS-Off Ver
    Excel 20(03|10|13)
    Posts
    2,581

    Re: Advanced Filter does not work in a recorded Macro

    You may also perform this manually but you must invoke the advanced filter with the destination sheet active.
    • Please remember to mark threads Solved with Thread Tools link at top of page.
    • Please use code tags when posting code: [code]Place your code here[/code]
    • Please read Forum Rules

  8. #8
    Registered User
    Join Date
    03-06-2008
    Location
    Ireland
    Posts
    26

    Re: Advanced Filter does not work in a recorded Macro

    Thanks for all the input.

    I am actually using two different sheets (InputSheet and OutputSheet). Where is this specified in the code please?
    Also the range I am checking is starting in cell 'F3' and could extend as far as cell 'F100' as in the code of my original post.

    Again, thank you all for the input.

  9. #9
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Advanced Filter does not work in a recorded Macro

    Can you tell us what you range you want to filter, where the criteria is and where you want the results?

  10. #10
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Advanced Filter does not work in a recorded Macro

    The code assumed that the current sheet is InputSheet. If that's not the case, select it at the beginning as follows.
    Sub Macro1()
        Sheets("InputSheet").Select
        Sheets("OutputSheet").Columns(1).ClearContents
        EndRw = Range("A" & Rows.Count).End(xlUp).Row
        Range("F1:F" & EndRw).AdvancedFilter Action:=xlFilterCopy, CopyToRange:=Sheets("OutputSheet").Range("A1"), Unique:=True
    End Sub
    Last edited by natefarm; 01-08-2014 at 01:47 PM. Reason: Code tags, and changed Col A to F

  11. #11
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Advanced Filter does not work in a recorded Macro

    natefarm

    I'm getting confused here, who is the OP and who isn't?

  12. #12
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Advanced Filter does not work in a recorded Macro

    What's an OP?

  13. #13
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Advanced Filter does not work in a recorded Macro

    Original Poster, the person who started the thread.

  14. #14
    Registered User
    Join Date
    03-06-2008
    Location
    Ireland
    Posts
    26

    Re: Advanced Filter does not work in a recorded Macro

    Thanks everyone for their help on this. I got it working eventually but it was a strange one.
    The code worked on some occasions but not on others. I have two hidden columns with data in them on tab 'InputSheet' and when I removed them the macro worked.
    As I required the data in the hidden columns for another macro, I put it on a new sheet and all is good now.
    Thanks again.

  15. #15
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Advanced Filter does not work in a recorded Macro

    That would make sense. A filter basically hides rows, so if some of your rows were already hidden, they would be excluded from the copy.
    Acts 4:12
    Salvation is found in no one else, for there is no other name under heaven given to mankind by which we must be saved.

  16. #16
    Valued Forum Contributor natefarm's Avatar
    Join Date
    04-22-2010
    Location
    Wichita, Kansas
    MS-Off Ver
    2016
    Posts
    1,020

    Re: Advanced Filter does not work in a recorded Macro

    Ok, I fixed it. Also changed from column A to F.
    Last edited by natefarm; 01-08-2014 at 01:45 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Advanced Filter Macro does not work, running manually works fine
    By jamiemc in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 09-09-2013, 07:22 PM
  2. Help! The recorded Macro does not work for the highlighted
    By lans_bazx in forum Excel Programming / VBA / Macros
    Replies: 30
    Last Post: 09-19-2012, 06:59 PM
  3. Recorded Macro Does Not Work
    By TheColorLavender in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-05-2012, 01:56 AM
  4. Second Advanced Filter does not work based on the data generated from the filter
    By mucc1928 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 08-29-2011, 02:42 PM
  5. advanced filter macro doesn't work on large data set
    By bullkater in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 12-02-2009, 08:05 AM

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