Unique array values

wisewood

Board Regular
Joined
Nov 7, 2002
Messages
193
Hi all,

I have an array, AccountManagers(x) containing, for example, 130 entries, of which there might only be something like 5-10 unique entries.

Can anyone tell me how i can pick out only the unique entries from this array?

Thanks.
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Code:
    Dim AccountManagers As Variant
    xVal = Sheets("Details").Cells(Rows.Count, 1).End(xlUp).Row
    ReDim AccountManagers(xVal)
    AccountManagers = Worksheets("Details").Range("D2:D" & xVal)
    Sheets("Summary").Range("S1").Value = UBound(AccountManagers)
    
    Dim coll As New Collection
 
    On Error Resume Next
    For i = LBound(AccountManagers) To UBound(AccountManagers)
        coll.Add AccountManagers(i), AccountManagers(i)
    Next i
    On Error GoTo 0
     
    Sheets("Summary").Cells(1, 21) = LBound(AccountManagers)
    Sheets("Summary").Cells(1, 22) = UBound(AccountManagers)
    Sheets("Summary").Cells(1, 23) = coll.Count
     
    'you now have a collection of the unique items from your array:
     
    For i = 1 To coll.Count
        Sheets("Summary").Cells(i, 20) = coll(i)
    Next i

Sorry, bit abrupt, i'm off into a meeting in a minute.
 
Upvote 0
I've come up with a bit of a cheat solution ...

I've added an extra column to the worksheet containing the account manager names and copied this formula down to all rows =COUNTIF($D$2:D2,D2).

This way, I can then loop through and only pick up the Account Manager names for the array where the count = 1.

Maybe not the most elegant solution, but it should do the job well enough.
 
Upvote 0
It was failing due to you having a 2 dimensional array - try the following modified code (note, your workaround is definitely not a bad solution, however!):

Code:
 Dim AccountManagers As Variant
    xVal = Sheets("Details").Cells(Rows.Count, 1).End(xlUp).Row
   ''' ReDim AccountManagers(xVal)  ====== Unnecessary
    AccountManagers = Worksheets("Details").Range("D2:D" & xVal)
    Sheets("Summary").Range("S1").Value = UBound(AccountManagers)
    
    Dim coll As New Collection
 
    On Error Resume Next
    For i = LBound(AccountManagers,1) To UBound(AccountManagers,1)
        coll.Add AccountManagers(i,1), AccountManagers(i,1)
    Next i
    On Error GoTo 0
     
    Sheets("Summary").Cells(1, 21) = LBound(AccountManagers)
    Sheets("Summary").Cells(1, 22) = UBound(AccountManagers)
    Sheets("Summary").Cells(1, 23) = coll.Count
     
    'you now have a collection of the unique items from your array:
     
    For i = 1 To coll.Count
        Sheets("Summary").Cells(i, 20) = coll(i)
    Next i
 
Upvote 0

Forum statistics

Threads
1,214,919
Messages
6,122,260
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top