List of Unique Data from Left portion of Range

spacecaptainsuperguy

Board Regular
Joined
Dec 30, 2004
Messages
202
Office Version
  1. 365
Platform
  1. Windows
I have a list of accounts structured as "Entity"-"Account#" with a dash in the middle such as ABC-123. The data on the left of the dash can be variable in length and contain both letters and numbers.

I'd like to extract a list of unique Entities (i.e. the left side of the dash). I was able to find the following code online which gets me close:

VBA Code:
Sub GetUnique_Collection() 'Using the Collection object
'
    Dim SourceRng As Range
    Dim UniqColl As New Collection
    Set SourceRng = Range("A2:A30")
    On Error Resume Next
    For Each cell In SourceRng.Cells
        UniqColl.Add cell.Value, cell.Value
    Next
    On Error GoTo 0
    ReDim UniqArray(1 To UniqColl.Count)
    For i = 1 To UniqColl.Count
        UniqArray(i) = UniqColl(i)
    Next
    'Optional sort routine can be inserted here
        Range("H1").Resize(UniqColl.Count, 1).Value = WorksheetFunction.Transpose(UniqArray)
        
End Sub

The catch is this code only works if the list had all the account numbers already removed. Rather than stripping off the account numbers and creating a whole new list to work from, I would like to try to build into this code to have it check the data left of the dash to see if it is unique or not. I'm guessing it would be this line of code that would need to be changed to do that?
UniqColl.Add cell.Value, cell.Value
But have no idea how to go about writing that out other than the formula to do so.

Any thoughts are much appreciated.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
What version of Excel are you using?

I suggest that you update your Account details (or click your user name at the top right of the forum) so helpers always know what Excel version(s) & platform(s) you are using as the best solution often varies by version. (Don’t forget to scroll down & ‘Save’)

Depending on your xl version this could easily be done with a formula.
 
Upvote 0
If your happy with a formula, how about
Excel Formula:
=UNIQUE(TEXTBEFORE(FILTER(A2:A100,A2:A100<>""),"-"))
 
Upvote 0
Done. Thanks for the tip.
Using Excel 365.
Reason I'm trying to squeeze this into a macro is that it is part of a bigger macro project, so the more of it I can automate the better.
 
Upvote 0
Ok, does the formula in post#3 work?
 
Upvote 0
Ok, how about
VBA Code:
Sub spacecaptain()
   Dim Ary As Variant
   Ary = Evaluate("UNIQUE(TEXTBEFORE(FILTER(A2:A100,A2:A100<>""""),""-""))")
   Range("H1").Resize(UBound(Ary)).Value = Ary
End Sub
 
Upvote 0
Solution
Ok, how about
VBA Code:
Sub spacecaptain()
   Dim Ary As Variant
   Ary = Evaluate("UNIQUE(TEXTBEFORE(FILTER(A2:A100,A2:A100<>""""),""-""))")
   Range("H1").Resize(UBound(Ary)).Value = Ary
End Sub
Like a charm.
Awesome. Thank you so much!
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,223,268
Messages
6,171,099
Members
452,379
Latest member
IainTru

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