Is there a faster/better way to accomplish scan of a range for unique text values/save those unique values to range, sort alphabetically?

johnnyL

Well-known Member
Joined
Nov 7, 2011
Messages
4,546
Office Version
  1. 2007
Platform
  1. Windows
Book1.xlsm
ABCDEFGHIJKLMNOPQRSTU
1
2
3Red146Green83Blue13Brown410Black25
4Blue21Purple27Red42Green61
5Green52Orange61Orange47
6Red05Purple83
7Yellow49
8Black1213
Sheet1


Back to this file ...

I want to capture unique text values from a range, sort that range, into the A column, and possibly even count the occurrences of each unique text value found into the corresponding B column.

The following is the code that I have came up with thus far ...

VBA Code:
Sub Find_PrintUniqueNonNumericTextFoundInSelectedRangeToRangeAndSortFoundTextAlphabetically()    ' Works
    Dim DataFoundInCell As Range, r As Range, x0
'
'   \/ Variables to set \/
    SortRange1stCellAddress = "C3"
    SortRangeLastCellAddress = "U8"
'
    With CreateObject("scripting.dictionary")
        For Each DataFoundInCell In ActiveSheet.Range(SortRange1stCellAddress & ":" & SortRangeLastCellAddress).SpecialCells(2)
            If Not IsNumeric(DataFoundInCell) Then x0 = .Item(DataFoundInCell.Value)      '
        Next
'
        Set r = Cells(1, "A").Resize(.Count, 1)                 '
'
        r.Value = Application.Transpose(.Keys)                  '
    End With
'
'   Sort Column A, that now contains the unique text values, alphabetically
'   \/ Variables to set \/
    SortRange1stCellAddress = "A1"
    SheetName = "Sheet1"
'
    SortRangeLastCellAddress = Range(SortRange1stCellAddress).End(xlDown).Address(0, 0)     '
'
    ActiveWorkbook.Worksheets(SheetName).Sort.SortFields.Clear
    ActiveWorkbook.Worksheets(SheetName).Sort.SortFields.Add Key:=Range(SortRange1stCellAddress), Order:=xlAscending    ' Predominant sort starting address
'
    With ActiveWorkbook.Worksheets(SheetName).Sort
        .SetRange Range(SortRange1stCellAddress & ":" & SortRangeLastCellAddress)
        .Header = xlNo
        .Apply
    End With
End Sub
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about
VBA Code:
Sub JonnyL()
   Dim Cl As Range
   
   With CreateObject("scripting.dictionary")
      For Each Cl In Range("C3:U8").SpecialCells(xlConstants, xlTextValues)
         .Item(Cl.Value) = .Item(Cl.Value) + 1
      Next Cl
      Range("A1").Resize(.Count, 2).Value = Application.Transpose(Array(.Keys, .Items))
      Range("A1").Resize(.Count, 2).Sort Range("A1"), xlAscending, , , , , , xlNo
   End With
End Sub
 
Upvote 0
Solution
Hi,​
can be also fast achieved just with Excel / VBA basics - different ways - without any Dictionary …​
 
Upvote 0
Glad to help & thanks for the feedback.
 
Upvote 0
Another question please, I try to digest what lines of code do so I can learn more. What does the following @Fluff line of code do:

VBA Code:
.Item(Cl.Value) = .Item(Cl.Value) + 1
 
Upvote 0
It adds the cell value to the dictionary as the key if it does not already exist & increases the item for that key by one.
 
Upvote 0
Eureka! Thank you @Fluff ! I was wondering how you did the counter. That explains it.
 
Upvote 0
You're welcome & thanks for the feedback.
 
Upvote 0
It adds the cell value to the dictionary as the key if it does not already exist & increases the item for that key by one.
From my googling, I think you can only store 1 set of unique keys and 1 set of corresponding items. What I was wanting to do now is get the unique keys and a running total that would be added from a column 2 to the right of the key found. For example in my OP above, the key would be from the 1st column (color column) and the total to add would be the 3rd column corresponding values.

So:
VBA Code:
Color Total Qty
Black     18
Red       13
Brown     10
Purple    10
Yellow     9
Orange     8
Green      6
Blue       4

If I have not googled enough to find a way to have the counter included also, like in the original solution then the following would be my goal:

VBA Code:
Color    Times Ordered    Total Qty
Green             3            6
Red               3           13
Black             2           18
Blue              2            4
Orange            2            8
Purple            2           10
Brown             1           10
Yellow            1            9

A column could be inserted, if need be, to have columns A - C for the result range.
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

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