+ Reply to Thread
Results 1 to 10 of 10

Using an autofilter instead of LOOP

  1. #1
    Registered User
    Join Date
    07-20-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    63

    Exclamation Using an autofilter instead of LOOP

    Hi All

    I have an spead sheet with 9 sheets 1 master and 8 sub sheets. The master sheet runs for col A to AV and the it can have x amount of rows. At the moment i use the following but this takes an age.
    Please Login or Register  to view this content.
    The first part removes any duplicates which i need but the next part loop looks for each name in col C copies the row and then paste it in the relevent sheet.

    I know this can be done using the a auto filter macro, but i can't get my head around these. Can someone please point me in the right direction.

    Also is there a set code that i could use for the auto filter and just change what i'm filtering by, i use the above loop often but its time consuming and inpractical.
    Attached Files Attached Files
    Last edited by E3iron; 02-11-2010 at 12:55 PM. Reason: To comply with forum rules

  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: Using an autofilter instead of LOOP

    You should come back and let us talk some about speeding up recorded macros by editing out the "selecting"...merging multiple lines of human activity recorded into one line of VBA command.

    Meanwhile, take a look a this stock macro of mine. It does what you're doing. The specifications of the macro are:

    1) Create a "list" in memory of all the unique values found in column A
    2) Put that list into a VBA array
    3) Going through the list one unique item at a time, use an AUTOFILTER on that data
    4) If a matching sheetname does not exist already, create it
    5) Copy the filtered data to the matching sheet
    6) Give a report of how many rows of data were copied as compared to how many rows there are TO copy, they should match!

    Since the macro brings over the headers from the original macro, too, we don't fiddle with that stuff.

    This should give you some ideas you can apply, or you can just tweak it to filter your sheet as is:
    Please Login or Register  to view this content.
    Last edited by JBeaucaire; 02-11-2010 at 11:55 AM. Reason: Added some comments to the code
    _________________
    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
    Registered User
    Join Date
    07-20-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Using an autofilter instead of LOOP

    Thanks for the reply, i think i got someway to go until i understand this code, but i will try line by line.

  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: Using an autofilter instead of LOOP

    If you state in words what you'd like your macro to do (don't refer to it as is), tweaking my macro to accommodate shouldn't be too hard.

    1) List unique values in column ??
    2) AutoFilter that column by each value and copy to sheet with same name
    3) During the copy, erase data on target sheet so copy is fresh each time

    Like that?

  5. #5
    Registered User
    Join Date
    07-20-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Using an autofilter instead of LOOP

    here we go.


    1) unique values, (Ian Parsons, Trevor Lees, Brian Hunt, kate Stapleton,Victoria Carpenter, Alex Hernandez, Matthew Thomas, Matthew Wells.

    2) use auto filter on column L

    3) filter each name and move in tab relating to that name Ian Parsons copy to Ian Parsons Tab, only copy don't delete.

    Also should i be using .activiate rather than .select

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

    Re: Using an autofilter instead of LOOP

    The macro recorder is recording your human activities. It doesn't give optimized code. I've merged as many as 10 lines of recorded code into a single line of direct VBA. And of course, eventually you'll discover ways to do things in a single line of VBA that the recorder could never suggest. For instance, most people loop through a column to delete blanks by evaluating the cells one at a time, but the SpecialCells feature can delete ALL of them in one step!

    I only use ACTIVATE if I want to take a shortcut in writing my commands. SELECT is eliminated 99% of the time (some printing VBA still wants things selected...very odd). As long as you qualify all the commands with parent objects, you can write and entire macro with no activating or selecting.

    If you want to see what I mean about merging, try this:

    1) Turn on the macro recorder
    2) Click on a different cell
    3) Type a word into that cell
    4) Click on that cell again and set the font to a different size
    5) Add a bottom edge border to the same cell
    6) Turn off the recorder

    Now look at the code. You only did 3 things, but you have 15-20 lines of code. Can you merge it down to only 3 things you did, thus 3 lines of code? Good exercise.
    Last edited by JBeaucaire; 02-11-2010 at 01:47 PM.

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

    Re: Using an autofilter instead of LOOP

    Quote Originally Posted by E3iron View Post
    here we go.

    1) unique values, (Ian Parsons, Trevor Lees, Brian Hunt, kate Stapleton,Victoria Carpenter, Alex Hernandez, Matthew Thomas, Matthew Wells.
    Oops, you misunderstood. A good macro won't require you to manually list the unique values, though you CAN, a good macro gets the unique values on its own. So, I would use the ADV FILTER on column L to get a list of the names used.

    This way, it's always automatic.

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

    Re: Using an autofilter instead of LOOP

    Here's the macro adjusted to use column L instead of A:
    Please Login or Register  to view this content.

  9. #9
    Registered User
    Join Date
    07-20-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Using an autofilter instead of LOOP

    Thank you very much, will this macro copy the information for L in to the relevent tab or will it creat new sheets. Also will it copy or cut the data from the master sheet.

  10. #10
    Registered User
    Join Date
    07-20-2009
    Location
    London
    MS-Off Ver
    Excel 2003
    Posts
    63

    Re: Using an autofilter instead of LOOP

    Sorry just tried this macro, it works and it brilliant. Thank you very much.

+ 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