Is this possible?

Rikki Tikki Tavi

New Member
Joined
Nov 2, 2023
Messages
12
Office Version
  1. 365
  2. 2021
Platform
  1. MacOS
HI there,

I have a 2020 mac with Microsoft 365 Excel, but I am newbie on ye olde Excel. I have spent hours on the Tube of You trying to figure this out. Not sure if anyone wants to take the time to indulge me but thought give it try! You can write this charity off on your taxes right?

How would I sort a column of cells that contain four numbers each. I want to sort the column not by the numeric value of the numbers in each cell, but by the numbers in the cell as a group, and by the frequency they occur in the column.
For example 2347, 1191, 2280, and then 2347 happens again BUT the set of numbers are NOT IN THAT ORDER, instead it's 4327. Lets say that out of 900 numbers 4327 appears 7 times, and that combination of numbers occurs a total of 24 times in different order.
However, the combination 4327 is repeated the most,7 x’s so it is sorted at the top of the column, 1-7.
Then the rest of the sets of 2347 are listed in order, by the number of times they appear, in places 8-24.
Then comes the second most repeated combination of numbers. Etc.
Can excel sort the column of numbers in this manner by the frequency those four numbers occur in every order they appear, and then move on to the second most frequent number and so on and so on?
Can I enter the data into each cell as four numbers or do I have to split the numbers up?

Any help would be greatly appreciated and I'll gladly email you a receipt for your trouble! 😜
 
No problem. I hope worked for you. However I am still unhappy with the some part of my code. Try like this. If it messes up, roll back to previous code.
VBA Code:
Sub test()
  Dim numbersDic As Object
  Dim numbers As Variant, i As Long, j As Long, k As Integer, c As Long
  Dim temp As Variant, tempArr As Variant
 
  With Application
  numbers = .Transpose(Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row))

  'First put unique numbers with their quantities in a dictionary
  'It will be useful to sort according to their appearance
  Set numbersDic = CreateObject("Scripting.Dictionary")
  For Each temp In numbers
    If Not numbersDic.Exists(temp) Then
      numbersDic.Add temp, countIfArray(temp, numbers)
    End If
  Next
  ReDim tempArr(1 To 2, 1 To 1)
  i = 1
 
  'Now transfer unique values to an array to play with
  For Each temp In numbersDic
    tempArr(1, i) = temp
    tempArr(2, i) = numbersDic(temp)
    i = i + 1
    ReDim Preserve tempArr(1 To 2, 1 To i)
  Next
  ReDim Preserve tempArr(1 To 2, 1 To i - 1)
 
  'Now sort in descending order according to their appearance
  For i = 1 To UBound(tempArr, 2) - 1
    For j = i + 1 To UBound(tempArr, 2)
      If tempArr(2, j) >= tempArr(2, i) Then
        For k = 1 To 2
          temp = tempArr(k, i)
          tempArr(k, i) = tempArr(k, j)
          tempArr(k, j) = temp
        Next
      End If
    Next
  Next

  'Now find out which are the combinations
  k = 1
  c = 1
  For i = 1 To UBound(tempArr, 2)
    If tempArr(1, i) <> "" Then
      For c = k To (k + tempArr(2, i)) - 1
        numbers(c) = tempArr(1, i)
      Next
      k = c - 1
      For j = i + 1 To UBound(tempArr, 2)
        If tempArr(1, j) <> "" Then
          If isCombination(tempArr(1, j), tempArr(1, i)) Then
            For c = k To (k + tempArr(2, j)) - 1
              numbers(c) = tempArr(1, j)
            Next
            tempArr(1, j) = ""
            k = c
          End If
        End If
      Next
    End If
  Next
  Range("B2").Resize(UBound(numbers)).Value = .Transpose(numbers)
  End With
End Sub
Function isCombination(ByVal tmp1 As Variant, ByVal tmp2 As Variant) As Boolean
  Dim temp3 As Variant, i As Integer, j As Integer
  tmp1 = Split(StrConv(tmp1, vbUnicode), Chr$(0))
  ReDim Preserve tmp1(UBound(tmp1) - 1)
  tmp2 = Split(StrConv(tmp2, vbUnicode), Chr$(0))
  ReDim Preserve tmp2(UBound(tmp2) - 1)
  tmp3 = tmp1
  For i = 0 To UBound(tmp1)
    For j = 0 To UBound(tmp2)
      If tmp1(i) = tmp2(j) Then
        tmp1(i) = 0
      End If
    Next
  Next
  For i = 0 To UBound(tmp2)
    For j = 0 To UBound(tmp3)
      If tmp2(i) = tmp3(j) Then
        tmp2(i) = 0
      End If
    Next
  Next
  If Evaluate(Join(tmp1, "+")) + Evaluate(Join(tmp2, "+")) = 0 Then
    isCombination = True
  End If
End Function
Function countIfArray(ByVal temp As Variant, ParamArray numbers() As Variant) As Integer
  Dim number As Variant
  For Each number In numbers(0)
    If number = temp Then countIfArray = countIfArray + 1
  Next
End Function
Okay, I'll give it a go - thank you very much for tweaking it!
 
Upvote 0

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Okay, I'll give it a go - thank you very much for tweaking it!
Really Very very very kind of you! I just copied this to the clipboard, now I'm going to try and get this into my computer. It may take me awhile to actually get this up and running in excel, probably a couple of days, but as soon as I do I will report back to you, thank you again for you hard work!
 
Upvote 0
I did get some wonderful advice, and marked the replies as solved.
now I'm going to try and get this into my computer.
The second quote here seems to indicate that you don't yet know if the suggestion in the marked solution is in fact a solution for your problem. If you don't yet know, please remove the marked solution for the moment at least.

Further, you stated that you are using Excel on a Mac, as your profile shows too. If that is the case, my understanding is that none of @Flashbond's suggestions will work on your machine, though I don't have a Mac to check for sure.

I put up that mini sheet.
You did, thanks. However my request asked for three things. The mini sheet appears to show the blue part but not the red (or is it the red part and not the blue?). The purple part is also missing.
Could you make up a small sample of, say, 20 to 30 rows and post that and the expected results done manually with XL2BB and explain again in relation to the sample data?
 
Upvote 0
The second quote here seems to indicate that you don't yet know if the suggestion in the marked solution is in fact a solution for your problem. If you don't yet know, please remove the marked solution for the moment at least.

Further, you stated that you are using Excel on a Mac, as your profile shows too. If that is the case, my understanding is that none of @Flashbond's suggestions will work on your machine, though I don't have a Mac to check for sure.


You did, thanks. However my request asked for three things. The mini sheet appears to show the blue part but not the red (or is it the red part and not the blue?). The purple part is also missing.
Well I see, back to the drawing board. I will remove the check mark and try to provide you with rest of the items you asked for. Perhaps I am over my head here...
 
Upvote 0
No problem. I hope worked for you. However I am still unhappy with the some part of my code. Try like this. If it messes up, roll back to previous code.
VBA Code:
Sub test()
  Dim numbersDic As Object
  Dim numbers As Variant, i As Long, j As Long, k As Integer, c As Long
  Dim temp As Variant, tempArr As Variant
 
  With Application
  numbers = .Transpose(Range("A2:A" & Cells(Rows.Count, "A").End(xlUp).Row))

  'First put unique numbers with their quantities in a dictionary
  'It will be useful to sort according to their appearance
  Set numbersDic = CreateObject("Scripting.Dictionary")
  For Each temp In numbers
    If Not numbersDic.Exists(temp) Then
      numbersDic.Add temp, countIfArray(temp, numbers)
    End If
  Next
  ReDim tempArr(1 To 2, 1 To 1)
  i = 1
 
  'Now transfer unique values to an array to play with
  For Each temp In numbersDic
    tempArr(1, i) = temp
    tempArr(2, i) = numbersDic(temp)
    i = i + 1
    ReDim Preserve tempArr(1 To 2, 1 To i)
  Next
  ReDim Preserve tempArr(1 To 2, 1 To i - 1)
 
  'Now sort in descending order according to their appearance
  For i = 1 To UBound(tempArr, 2) - 1
    For j = i + 1 To UBound(tempArr, 2)
      If tempArr(2, j) >= tempArr(2, i) Then
        For k = 1 To 2
          temp = tempArr(k, i)
          tempArr(k, i) = tempArr(k, j)
          tempArr(k, j) = temp
        Next
      End If
    Next
  Next

  'Now find out which are the combinations
  k = 1
  c = 1
  For i = 1 To UBound(tempArr, 2)
    If tempArr(1, i) <> "" Then
      For c = k To (k + tempArr(2, i)) - 1
        numbers(c) = tempArr(1, i)
      Next
      k = c - 1
      For j = i + 1 To UBound(tempArr, 2)
        If tempArr(1, j) <> "" Then
          If isCombination(tempArr(1, j), tempArr(1, i)) Then
            For c = k To (k + tempArr(2, j)) - 1
              numbers(c) = tempArr(1, j)
            Next
            tempArr(1, j) = ""
            k = c
          End If
        End If
      Next
    End If
  Next
  Range("B2").Resize(UBound(numbers)).Value = .Transpose(numbers)
  End With
End Sub
Function isCombination(ByVal tmp1 As Variant, ByVal tmp2 As Variant) As Boolean
  Dim temp3 As Variant, i As Integer, j As Integer
  tmp1 = Split(StrConv(tmp1, vbUnicode), Chr$(0))
  ReDim Preserve tmp1(UBound(tmp1) - 1)
  tmp2 = Split(StrConv(tmp2, vbUnicode), Chr$(0))
  ReDim Preserve tmp2(UBound(tmp2) - 1)
  tmp3 = tmp1
  For i = 0 To UBound(tmp1)
    For j = 0 To UBound(tmp2)
      If tmp1(i) = tmp2(j) Then
        tmp1(i) = 0
      End If
    Next
  Next
  For i = 0 To UBound(tmp2)
    For j = 0 To UBound(tmp3)
      If tmp2(i) = tmp3(j) Then
        tmp2(i) = 0
      End If
    Next
  Next
  If Evaluate(Join(tmp1, "+")) + Evaluate(Join(tmp2, "+")) = 0 Then
    isCombination = True
  End If
End Function
Function countIfArray(ByVal temp As Variant, ParamArray numbers() As Variant) As Integer
  Dim number As Variant
  For Each number In numbers(0)
    If number = temp Then countIfArray = countIfArray + 1
  Next
End Function
I did check those boxes on my profile that said I was on a mac but for some reason it didn't get posted with my avatar, very sorry for that! Thank you so much for your work on my behalf...
 
Upvote 0
I absolutely can, thank you!

Well I see, back to the drawing board. I will remove the check mark and try to provide you with rest of the items you asked for. Perhaps I am over my head here...

This is not the raw data.
It is in the final form that I would like it in.
The raw data looks exactly the same, but not in any order. The raw data consists of around 1500 cells of four numbers, but the order is completely random. I want to organize the numbers in the following manner as illustrated below:

1. By overall frequency occurring in the column. So 4327 occurs 18 times, it's occurs most frequently of the total 1500 numbers in the column.
2. I organize that set by frequency of the order they occur. 4327 occurs 8 times, so it's the most frequent, its at the top, then each set is organized in descending order
*If all that can be done is that the same four numbers can be grouped together that would be fine, brilliant really.
So 4327, 3427, 7342 - if those could end up all together in the column GREAT! I can organize them from there myself!

Sample Book2.xlsx
AB
14327
24327
34327
44327
54327
64327
74327
84327
92347
102347
112347
122347
132347
143472
153472
163472
177243
187243
191190
201190
211190
221190
231190
241190
251190
261091
271091
281091
291091
309011
319011
329011
339011
341019
351019
363112
373112
383112
393112
401213
411213
422113
432113
441123
451123
465325
475325
485532
495532
502355
515235
525325
536758
548756
555768
566578
577865
589432
592943
604932
61
Sheet1
 
Upvote 0
With your 60 rows of data, what are your expected results?
With the stated numbers, the first few are

T202311a.xlsm
DE
243278
311907
423475
1d
Cell Formulas
RangeFormula
D2:D25D2=SORTBY(B2#,C2:C25,-1)
E2:E4E2=COUNTIFS(rN,D2)
Dynamic array formulas.
Named Ranges
NameRefers ToCells
rN='1d'!$A$2:$A$61E2:E4
 
Upvote 0
Post #17 Please ignore the formulas part.

I was trying to determine the results that you require.
Sort results by largest count
4327843278
1190711907
2347523475
1091490114
9011431124
3112410914
3472353253
5325334723
 
Upvote 0
I want to organize the numbers in the following manner as illustrated below:

1. By overall frequency occurring in the column. So 4327 occurs 18 times, it's occurs most frequently of the total 1500 numbers in the column.
2. I organize that set by frequency of the order they occur. 4327 occurs 8 times, so it's the most frequent, its at the top, then each set is organized in descending order
Thanks for the further clarification. See if this does what you want.
Just for visual checking I have manually coloured the major groupings in column C and the sub-groups in column D.

Rikki Tikki Tavi.xlsm
ABCD
112134327
234724327
353254327
429434327
511904327
623474327
711904327
843274327
911902347
1011902347
1165782347
1211232347
1323472347
1443273472
1572433472
1652353472
1710917243
1810917243
1990111190
2043271190
2134721190
2212131190
2343271190
2431121190
2587561190
2610919011
2711239011
2853259011
2923479011
3043271091
3111901091
3231121091
3310191091
3478651019
3555321019
3690113112
3772433112
3857683112
3955323112
4023472113
4190112113
4210911213
4323471213
4410191123
4511901123
4643275325
4734725325
4831125325
4921135532
5094325532
5131125235
5211902355
5367588756
5449327865
5543276758
5653256578
5790115768
5843279432
5921134932
6023552943
Sheet3
Cell Formulas
RangeFormula
C1:C60C1=LET(a,A1:A60,x,--BYROW(a,LAMBDA(rw,CONCAT(SORT(MID(rw,SEQUENCE(4),1))))),SORTBY(a,INDEX(FREQUENCY(x,SEQUENCE(MAX(x))),x),-1,COUNTIF(a,a),-1,a,-1))
Dynamic array formulas.
 
Upvote 0
Solution

Forum statistics

Threads
1,223,903
Messages
6,175,284
Members
452,630
Latest member
OdubiYouth

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