[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
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,298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a serious pitfall that can happen when deleting shapes using VBA.
If you need to forecast numbers -- typically for finance -- the Windows and Mac versions of Excel 2016 have a basket of tools to get the job done.
This Micro Tutorial will demonstrate on a Mac how to change the sort order for chart legend values and decrpyt the intimidating chart menu.
This Micro Tutorial will demonstrate in Google Sheets how to use the HYPERLINK function to create live links inside your spreadsheet.

650 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