+ Reply to Thread
Results 1 to 22 of 22

VBA Code to apply criteria to a filter

  1. #1
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    VBA Code to apply criteria to a filter

    My code for a filter isn't working and i'm not sure why;

    I'm trying to apply a filter to show 2 things in the same column

    Please Login or Register  to view this content.
    when i run the code, nothing appears.
    Last edited by kenadams378; 08-13-2012 at 05:29 AM.

  2. #2
    Forum Contributor
    Join Date
    09-11-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    102

    Re: VBA Code to apply criteria to a filter

    slt,
    try to use this code:
    Worksheets("Sheet1").cells.AutoFilter Field:=2, Criteria1:="=GBPMORCA", Operator:=xlOr, Criteria2:="=GBPMOECA"
    or
    Worksheets("Sheet1").cells.AutoFilter 2,"GBPMORCA",xlor,"GBPMOECA"

  3. #3
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    My sheet just remains blank,

    This is the whole code which opens the file, applies the filter, copies the data to another sheet.

    Please Login or Register  to view this content.

  4. #4
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA Code to apply criteria to a filter

    Can you attach the sample?
    If I have helped, Don't forget to add to my reputation (click on the star below the post)
    Don't forget to mark threads as "Solved" (Thread Tools->Mark thread as Solved)
    Use code tags when posting your VBA code: [code] Your code here [/code]

  5. #5
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    Can't file it over 30mb.

    The workbook has the following sheets;

    Start
    Original Morca
    Orignal Moeca
    S29 Data

    Full code as follows;

    Please Login or Register  to view this content.

  6. #6
    Forum Contributor
    Join Date
    09-11-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    102

    Re: VBA Code to apply criteria to a filter

    Bonjour,
    I don't know data in your spreatsheet. but you can follow structure below:
    Worksheet("sheet1").range("region for autofilter..eg A1:D65536...").Autofiler, Field:=???, Criteria1:="=GBPMORCA", Operator:=xlOr, Criteria2:="=GBPMOECA"
    Field : is a number, that is location of column containing two values of criteria..GPMORCA and GBPMPECA
    'Sorry I don't speak english well.'

  7. #7
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    Still doesn't work....

  8. #8
    Forum Contributor
    Join Date
    09-11-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    102

    Re: VBA Code to apply criteria to a filter

    slt,
    you can see an example below
    Attached Files Attached Files

  9. #9
    Forum Contributor
    Join Date
    09-11-2009
    Location
    Paris
    MS-Off Ver
    Excel 2003
    Posts
    102

    Re: VBA Code to apply criteria to a filter

    if you want to copy data after autofilter. you have to put code lrow after
    Worksheets("Sheet1").Cells.AutoFilter Field:=2, Criteria1:="=GBPMORCA", Operator:=xlOr, Criteria2:="=GBPMOECA"
    lrow = Workbooks(FName).Worksheets("Sheet1").Range("A" & Rows.Count).End(xlUp).Row

  10. #10
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA Code to apply criteria to a filter

    Have you tried without the "=" sign in front of the criteria? What error do you get?

  11. #11
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    I don't get an error, it just doesn't copy the data from the opened file into the tab to filter...

  12. #12
    Forum Contributor arlu1201's Avatar
    Join Date
    09-09-2011
    Location
    Bangalore, India
    MS-Off Ver
    Excel 2003 & 2007
    Posts
    19,166

    Re: VBA Code to apply criteria to a filter

    It will be good if you attach a sample file.

  13. #13
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    The smallest i can get the file down to is 11mb

  14. #14
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    Right, i have created 4 sheets which represent each tab in thew live workbook, this runs with the code listed above.
    Attached Files Attached Files

  15. #15
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Code to apply criteria to a filter

    are you sure field 2 is the one you want? (none of the sample files contain the text you are filtering for which doesn't help us) you also did not fully qualify the source sheet. perhaps
    Please Login or Register  to view this content.
    Last edited by JosephP; 08-10-2012 at 05:20 AM.
    Josie

    if at first you don't succeed try doing it the way your wife told you to

  16. #16
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    Sorry i should have edited the code, it is field 19 i require then selecting GBPMORCA & GBPMOECA

    ---------- Post added at 10:32 AM ---------- Previous post was at 10:28 AM ----------

    I get the following error on this code

    [code]
    .AutoFilter Field:=19, Criteria1:="GBPMORCA", Operator:=xlOr, Criteria2:="GBPMOECA"
    [/code

    Autofilter method of range class failed

  17. #17
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Code to apply criteria to a filter

    my bad-the filter range needs to be wider. replace this part
    Please Login or Register  to view this content.
    with
    Please Login or Register  to view this content.

  18. #18
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    I have got it to work now, just needed to extend the range of the copy before the filter.

    Is there a way i can trim this macro down at all? Is it possible to use an array to open the files and copy data etc?

    Trying to find the most efficient code to run this process.

  19. #19
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Code to apply criteria to a filter

    you can use an array for the first two but the process is different for the third workbook.

  20. #20
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    How do i incorporate an array into the first two steps in the process?

    Arrays confuse the life out of me?

  21. #21
    Forum Guru JosephP's Avatar
    Join Date
    03-27-2012
    Location
    Ut
    MS-Off Ver
    2003/10
    Posts
    7,328

    Re: VBA Code to apply criteria to a filter

    Please Login or Register  to view this content.

  22. #22
    Forum Contributor
    Join Date
    05-01-2012
    Location
    London
    MS-Off Ver
    Excel 2010
    Posts
    822

    Re: VBA Code to apply criteria to a filter

    Many many thanks...

+ 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