?
Solved

Excel VBA 2010 - how to create a single dimension with unique values array from table column

Posted on 2014-02-10
3
Medium Priority
?
2,311 Views
Last Modified: 2014-02-11
I have a workbook that has a defined table on a sheet.  I want to get the unique values from one of the columns in the table and put those unique values in an array so I can loop over them.  A blank value should count as a unique value.   How can this be accomplished? My table name is T_201311 and the column name is EnterpriseID.
0
Comment
Question by:mamuscia
  • 2
3 Comments
 
LVL 81

Expert Comment

by:byundt
ID: 39848915
The easiest way to get a list of unique values is using Advanced Filter on that column and writing the unique results to a worksheet range. Here is a code snippet that uses that method to put the unique values in an empty column to the right of the used range. As written, it hides that column and creates a named range that points to the list of unique items (excluding header).
    Set rg = .UsedRange
    Set rg = rg.Cells(1, 1).Offset(0, rg.Columns.Count + 1)
    .Range("A6").CurrentRegion.Columns(1).AdvancedFilter xlFilterCopy, , CopyToRange:=rg, Unique:=True
    Set rg = rg.CurrentRegion
    Set rg = rg.Offset(1, 0).Resize(rg.Rows.Count - 1)
    rg.EntireColumn.Hidden = True

Open in new window


Another way to do it is to create a dictionary object, which doesn't tolerate duplicates.
0
 
LVL 81

Accepted Solution

by:
byundt earned 2000 total points
ID: 39848956
Here is a sub & function using the Scripting Dictionary collection to make a list of the unique values in your table. The function returns the unique items in an array.

Note that I set a reference to "Microsoft Scripting Runtime" by checking its box in the Tools...Reference menu item.
Function Uniques(sTableName As String, sColumnName As String) As Variant
Dim dic As Scripting.Dictionary
Dim i As Long, n As Long
Dim v As Variant, vData As Variant, vResults As Variant
Dim tbl As ListObject
Dim col As ListColumn
Set dic = CreateObject("Scripting.Dictionary")
With ActiveSheet
    Set tbl = .ListObjects(sTableName)
    Set col = tbl.ListColumns(sColumnName)
    vData = col.DataBodyRange.Value
End With
On Error Resume Next
For Each v In vData
    dic.Add v, v
Next
On Error GoTo 0
    n = dic.Count
ReDim vResults(1 To n)
For i = 1 To n
    vResults(i) = dic.Items(i - 1)
Next
Uniques = vResults
End Function

Sub Test()
Dim v As Variant
v = Uniques("T_201311", "EnterpriseID")
Range("Q1").Resize(UBound(v)).Value = Application.Transpose(v)
End Sub

Open in new window

0
 

Author Closing Comment

by:mamuscia
ID: 39850071
thanks. this works and gives me the array I wanted.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Freeze panes is an option within all variants of Excel to enable parts of a sheet to remain stationary when the cursor is in another part of the sheet. This is a very useful feature which is overlooked or under used.
This article describes a serious pitfall that can happen when deleting shapes using VBA.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question