Autofilter using unique values required in Excel

theshizy

The code below gets a list of unique contacts from column F and then outputs them in the Immediate window.

How would I use this data created to run a series of autofilters on the data (i.e. within a for loop, one at a time). In between each autofilter, the data will be copied and saved to a new spreadsheet.

Sub GetPrimaryContacts()

Dim Col As New Collection
Dim itm
Dim i As Long
Dim CellVell As Variant

'Get last row value
LastRow = Cells.SpecialCells(xlCellTypeLastCell).Row

'Loop between all rows to get unique values
For i = 3 To LastRow
    CellVal = Sheets("Master").Range("F" & i).Value
    On Error Resume Next
    Col.Add CellVal, Chr(34) & CellVal & Chr(34)
    On Error GoTo 0
Next i

For Each itm In Col
    Debug.Print itm
Next

End Sub
pintxo

Pseudo Code, sorry I haven't used VBA for a while:

dictionary as Dictionary

for each row in sheet
  attribute = Cell(A,1)
  if (attribute not in dictionary) then
    dictionary.put(attribute, newWorkbook)
  end if
  workbook = dictionary.get(attribute)
  'copy row to workbook
end for

This code could be highly optimized by first storing the original data in a array of type Variable and also storing the outputs for the diverse workbooks in arrays of type Variable. Only once the loop is finished you actually create the output workbooks and write the contents from the arrays to the workbooks.

That way we do not need to first look for the unique values and then repeatedly apply the AutoFilter which will be a lot slower and less easy to maintain.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Autofilter excel using java poi

From Dev

excel autofilter using xlAnd and xlOr

From Dev

VBA: AdvancedFilter to get unique values after AutoFilter

From Dev

Excel VBA copying rows using autofilter

From Dev

Excel : Count unique values using conditions and combos

From Dev

Recalculate unique rank with autofilter?

From Dev

Find unique values in Excel

From Dev

COUNTIFS with unique values Excel

From Dev

Protect Excel worksheet while keeping autofilter, using Spreadsheet gear

From Dev

How do I loop through an Autofilter using VBA in excel?

From Dev

Using VBA Autofilter to exclude all values between two numbers

From Dev

How to get unique values in a column using excel formula

From Dev

How to filter unique values in a column using excel formula

From Dev

Excel: Index Match using criteria, show unique values?

From Dev

Extract unique values from an SQL database to excel using python

From Dev

Detect autofilter on Excel Tables

From Dev

Set AutoFilter in Excel with VBA

From Dev

Excel autofilter errors

From Dev

Excel VBA AutoFilter Codes

From Dev

Excel VBA AutoFilter Percentages

From Dev

Counting unique values in R and excel

From Dev

Use Excel countifs with unique values

From Dev

Excel unique values multiple rows

From Dev

Excel Return Multiple Unique Values

From Dev

Using multiple criteria on Autofilter

From Dev

Excel: get list of unique values from a table and place the values in one column using formulas

From Dev

Passing Excel rows as an array to autofilter

From Dev

Excel VBA autofilter all but three

From Dev

Autofilter on Mutliple Columns Excel VBA