Counting the number of different duplicates in a list

francorbusetti

New Member
Joined
Dec 14, 2005
Messages
13
I have a list (column, say) of numbers. They will be recalculated continuously as part of an optimization routine.
I need to generate another list (column) that counts the number of different duplicates in the original list, preferably ranked. (This is what the Tally command does in Mathematica.)
e.g. {21 7 3 7 7 3} => {3 2 1}
{21 7 3 7 21 2} => {2 2 1 1}
{21 7 3 2 1 0} => {1 1 1 1 1 1}
Any ideas?
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
Hi
If you data in columns (A,B,C)
The result in colums (B,D,F)

Try this (but the result not ranked)
Code:
Sub tester()
    Dim a, x, i, k, itm, j, lr
    ReDim x(1 To 7)
    For j = 1 To 6 Step 2
        With CreateObject("scripting.dictionary")
            For i = 1 To Cells(Rows.Count, j).End(xlUp).Row - 1
                a = Application.Transpose(Range(Cells(2, j), Cells(7, j)))
                If Not .exists(a(i)) Then
                    .Add a(i), 1
                Else
                    .Item(a(i)) = .Item(a(i)) + 1
                End If
            Next
            Cells(2, j + 1).Resize(.Count) = Application.Transpose(.items)
            .RemoveAll
        End With
    Next
End Sub
 
Last edited:
Upvote 0
Try this (Ranked)
Code:
Sub tester()
    Dim a, x, i, k, itm, j, lr
    Dim rng As Range
    ReDim x(1 To 7)
    For j = 1 To 6 Step 2
        With CreateObject("scripting.dictionary")
            For i = 1 To Cells(Rows.Count, j).End(xlUp).Row - 1
                a = Application.Transpose(Range(Cells(2, j), Cells(7, j)))
                If Not .exists(a(i)) Then
                    .Add a(i), 1
                Else
                    .Item(a(i)) = .Item(a(i)) + 1
                End If
            Next
            Cells(2, j + 1).Resize(.Count) = Application.Transpose(.items)
            Set rng = Cells(2, j + 1).Resize(.Count)
            ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Clear
            ActiveWorkbook.Worksheets("Sheet1").Sort.SortFields.Add Key:=rng, _
                                                                    SortOn:=xlSortOnValues, Order:=xlDescending, DataOption:=xlSortNormal
            With ActiveWorkbook.Worksheets("Sheet1").Sort
                .SetRange rng
                .Header = xlGuess
                .MatchCase = False
                .Orientation = xlTopToBottom
                .SortMethod = xlPinYin
                .Apply
            End With
            .RemoveAll
        End With
    Next
End Sub
 
Last edited:
Upvote 0
Thanks, appreciated. However, since the sheet is being recalculated continuously one can't use code. For the same reason one can't, for example, do the sorting manually.
Something like FREQUENCY is required, except there are no bins, only the actual values in the list.
 
Last edited:
Upvote 0
Hi
see if this meets your requirement
Sorry for the delay
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Dim a, x, i, k, itm, j, lr
    ReDim x(1 To 7)
        With CreateObject("scripting.dictionary")
            For i = 1 To Cells(Rows.Count, 1).End(xlUp).Row - 2
                a = Application.Transpose(Range(Cells(2, 1), Cells(7, 1)))
                If Not .exists(a(i)) Then
                    .Add a(i), 1
                Else
                    .Item(a(i)) = .Item(a(i)) + 1
                End If
            Next
            Cells(2, 2).Resize(.Count) = Application.Transpose(.items)
            .RemoveAll
        End With
End Sub

This code should go to the sheet code (not module)
 
Last edited:
Upvote 0
Cross posted https://www.excelforum.com/excel-ge...number-of-different-duplicates-in-a-list.html

While we do not prohibit Cross-Posting on this site, we do ask that you please mention you are doing so and provide links in each of the threads pointing to the other thread (see rule 13 here along with the explanation: Forum Rules).
This way, other members can see what has already been done in regards to a question, and do not waste time working on a question that may already be answered.
 
Upvote 0

Forum statistics

Threads
1,214,985
Messages
6,122,606
Members
449,089
Latest member
Motoracer88

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