0

I am regularly receiving a raw export file and I have written macros to format and manipulate it. I use Excel 2010 and I'm moderately comfortable in VBA.

One of the incoming columns has a list of names, from a known (finite but large) list, separated by semicolons. If you use Autofilter, though, it doesn't treat the entries like true multi-selects.

Question: Can I programmatically tell Excel to see "apple; grape" as a multi-select of both "apple" and "grape"?

Example:

Assume that we know the list only contains the following names: apple cherry grape orange peach

The cell(s) in Column (A) might contain one, two, ...or five of these fruits in any combination, separated by ";".

(A1) = apple (A2) = apple; grape (A3) = apple; peach (A4) = cherry; grape (A5) = cherry; orange

Luckily for me, the incoming data is always exported in alphabetical order (so I won't ever have one cell containing "apple; grape" and another cell containing "grape; apple").

The Problem: AutoFilter assumes that each possible combination is a unique 'select' option. In later steps, I need to combine/filter based on multiple criteria that make it complicated to use an array - because I have too large a list to view all the combination permutations as unique possibilities.

[For example: I am filtering to copy a group of data to a new tab, and sometimes I'm just moving anything with "grape" to a new tab, but sometimes anything that contains "grape" OR "cherry" gets copied into the same new tab.]

There is a distinct possibility that I'm overthinking this and making it harder than it has to be!

Thanks!

Edit: In other words, I don't see how to reasonably loop through Range().Autofilter Field... Criteria1:=() with an exponentially large set of combinations. Make sense?

3
  • So what code have you got so far, and where are you getting stuck? Aug 9, 2013 at 17:50
  • I know how to (in VBA) create a validated list & assign it to drop downs in a column of cells. I found some code on a multi-select list @ blog.contextures.com/archives/2011/04/29/… but while I can usually sniff out what I need from others' code, I'm at a bit of a loss on where to start. Will I be moving the existing data into a new column? Aug 9, 2013 at 18:23
  • I think I'm in over my head because I'm trying to be efficient. I'm using an array with a for/next to create & name a tab for each fruit (using this example), and creating the drop down list again on each tab. But in my head, I don't see how to efficiently (a) use each fruit in the list as a filter on the main page, (b) copy the resulting filtered list to the correctly named tab, (c) go back to the main page, clear the filter, and filter for the next fruit OR fruits (because some of the "filters" are compound/multi items). The singles I get- multiples I should just do outside of the loop? Aug 9, 2013 at 18:34

1 Answer 1

2

Use advancedfilter instead of autofilter since it can handle as many criterias as you want.
Here is a working example. Set up your Excel sheet like the first screenshot is showing and run the macro.

Before macro
enter image description here

Sub AdvancedFilterTest()

    'clear old results and old filters
    Range("C:C").Clear
    Range("D:D").Clear

    'criteria header name and data header name needs to be the same
    [C1] = [B1]

    'Split multiple criterias by semicolon and save them as array
    Criterias = Split([A2], ";")

    'write the array to cells since advancedfilter needs a range as criteria
    For i = 0 To UBound(Criterias)
        Cells(i + 2, 3) = Criterias(i)
    Next i

    'Set the cells as a range so advancedfilter can use them
    Set critrange = Range(Cells(1, 3), Cells(UBound(Criterias) + 2, 3))

    'advanced filter will filter and copy your data to a new target range
    Range("B:B").AdvancedFilter Action:=xlFilterCopy, CriteriaRange:=critrange, CopyToRange:=Sheets(1).[D1]

End Sub

After macro
enter image description here

I'm sure you will get the idea and how to adapt it.

1
  • Nixda, thank you! I can't believe I've never played with this before. It even has the bit to copy to another page integrated. Excellent! Aug 12, 2013 at 20:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .