Sort a list of combinations based on the numerical values from each cell

vladimiratanasiu

Active Member
Joined
Dec 17, 2010
Messages
347
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Hello!
I have a range of cells with numerical combinations (A8:A38) equal with a given sum (A7), generated on the basis of a built-in list (A1:B5). Each combination contains in this case 3-5 values, but its length may depend on the size of range and the target number. I need a formula, macro etc., to sort all combinations in ascending series, according to the number of elements (1,2,3 etc.) from each cell, and if possible the content of that cell to be sorted from 1 to 9.

Thank you!
Book1.xlsm
AB
116
227
338
449
5510
6
720
81,6,2,7,4
91,6,2,3,8
101,6,3,10
111,6,8,5
121,6,4,9
131,2,7,10
141,2,3,4,10
151,2,3,9,5
161,2,8,4,5
171,2,8,9
181,7,3,4,5
191,7,3,9
201,7,8,4
211,4,5,10
221,9,10
236,2,7,5
246,2,3,4,5
256,2,3,9
266,2,8,4
276,7,3,4
286,4,10
296,9,5
302,7,3,8
312,3,5,10
322,8,10
332,4,9,5
347,3,10
357,8,5
367,4,9
373,8,4,5
383,8,9
Sheet1
Cell Formulas
RangeFormula
A8:A38A8=TRANSPOSE(FindSumCombinations(A1:B5,A7))
Dynamic array formulas.
 
Last edited:

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
What is FindSumCombinations?
It's a custom function, based on the macro from Find all combinations of numbers that equal a given sum in Excel. I used it to find all combinations of the numbers from the range A1:B5 that can be added up in order to get the value from cell A7, and the respective list was automatically generated in the range A8:A38. Now, I need a solution to group / organize in ascending order the combinations as they are in the column E8:E38, based on the number of elements from each cell (in our case 3, 4 and 5 ones), and and if possible the values to be sorted from 1 to 9 in each cell. If it's too difficult to get results from E8:E38, they can be sorted like in the range C8:C38, keeping only the ascending order for the number of the elements.

Thank you!

Auto arrange.xlsx
ABCDE
116
227
338
449
5510
6
720
81,2,3,4,101,9,101,9,10
91,2,3,9,52,8,102,8,10
101,2,7,103,8,93,7,10
111,2,8,4,56,4,103,8,9
121,2,8,96,9,54,6,10
131,4,5,107,3,104,7,9
141,6,2,3,87,4,95,6,9
151,6,2,7,47,8,55,7,8
161,6,3,101,2,7,101,2,7,10
171,6,4,91,2,8,91,2,8,9
181,6,8,51,4,5,101,3,6,10
191,7,3,4,51,6,3,101,3,7,9
201,7,3,91,6,4,91,4,5,10
211,7,8,41,6,8,51,4,6,9
221,9,101,7,3,91,4,7,8
232,3,5,101,7,8,41,5,6,8
242,4,9,52,3,5,102,3,5,10
252,7,3,82,4,9,52,3,6,9
262,8,102,7,3,82,3,7,8
273,8,4,53,8,4,52,4,5,9
283,8,96,2,3,92,4,6,8
296,2,3,4,56,2,7,52,5,6,7
306,2,3,96,2,8,43,4,5,8
316,2,7,56,7,3,43,4,6,7
326,2,8,41,2,3,4,101,2,3,4,10
336,4,101,2,3,9,51,2,3,5,9
346,7,3,41,2,8,4,51,2,3,6,8
356,9,51,6,2,3,81,2,4,5,8
367,3,101,6,2,7,41,2,4,6,7
377,4,91,7,3,4,51,3,4,5,7
387,8,56,2,3,4,52,3,4,5,6
Sheet1
 
Upvote 0
Why don't you do the required sorting in the macro, before it returns the result?
 
Upvote 0
Why don't you do the required sorting in the macro, before it returns the result?
To be honest, I'm not very skilful in adjusting the complex macros. That's why, I'd be grateful if you / anyone else could help me with this / other solution, in order to solve my problem.
Thank you!
 
Last edited:
Upvote 0
This is the code I used in the custom function FindSumCombinations. If someone could adjust it to my needs, I will be very thankful.

VBA Code:
Option Explicit

Public Function FindSumCombinations(rngNumbers As Range, lTargetSum As Long)
 Dim arNumbers() As Long, part() As Long
 Dim arRes() As String
 Dim indI As Long
 Dim cellCurr As Range
 
 ReDim arRes(0)
 If rngNumbers.Count > 1 Then
  ReDim arNumbers(rngNumbers.Count - 1)
 
  indI = 0
  For Each cellCurr In rngNumbers
   arNumbers(indI) = CLng(cellCurr.Value)
   indI = indI + 1
  Next cellCurr
  Call SumUpRecursiveCombinations(arNumbers, lTargetSum, part(), arRes())
 End If
 ReDim Preserve arRes(0 To UBound(arRes) - 1)
 FindSumCombinations = arRes
End Function
 

Private Sub SumUpRecursiveCombinations(Numbers() As Long, target As Long, part() As Long, ByRef arRes() As String)
 Dim s As Long, i As Long, j As Long, num As Long, indRes As Long
 Dim remaining() As Long, partRec() As Long
 s = SumArray(part)
 
 If s = target Then
  indRes = UBound(arRes)
  ReDim Preserve arRes(0 To indRes + 1)
  arRes(indRes) = ArrayToString(part)
 End If
 If s > target Then Exit Sub

 If (Not Not Numbers) <> 0 Then
  For i = 0 To UBound(Numbers)
   Erase remaining()
   num = Numbers(i)
   For j = i + 1 To UBound(Numbers)
    AddToArray remaining, Numbers(j)
   Next j
   Erase partRec()
   CopyArray partRec, part
   AddToArray partRec, num
   SumUpRecursiveCombinations remaining, target, partRec, arRes
  Next i
 End If
End Sub


Private Function ArrayToString(x() As Long) As String
 Dim n As Long, result As String
 result = x(n)
 For n = LBound(x) + 1 To UBound(x)
  result = result & "," & x(n)
 Next n
 ArrayToString = result
End Function


Private Function SumArray(x() As Long) As Long
 Dim n As Long
 SumArray = 0
 If (Not Not x) <> 0 Then
  For n = LBound(x) To UBound(x)
   SumArray = SumArray + x(n)
  Next n
 End If
End Function
 
Last edited:
Upvote 0
Can you have a helper column, and have an additional area for sorted results? Actually, does your version of Excel have the SORT function? If so, does putting that around your function work?
 
Upvote 0
Can you have a helper column, and have an additional area for sorted results? Actually, does your version of Excel have the SORT function? If so, does putting that around your function work?
Yes, I used the SORT function, but the results are not OK. As you can see, after sorting, the combinations are not arranged in distinct groups with 3, 4 and 5 elements, but mixed together. On the other hand, the numerical content from cells keeps the ascending order only with the first two digits (1,1; 1,2; 1,3 etc.), but fails in the case of the whole combination (e.g. 1,2,8,4,5 vs. 1,2,4,5,8). Maybe there are other formulas more complex, that can take into account my criteria, but I don't know them. Any other working solution will be very appreciated.
Thank you!
Book1.xlsm
ABCDEFG
116
227
338
449
5510
6
720
81,6,2,7,41,2,3,4,10VS.1,9,10VS.1,9,10
91,6,2,3,81,2,3,9,52,8,102,8,10
101,6,3,101,2,7,103,8,93,7,10
111,6,8,51,2,8,4,56,4,103,8,9
121,6,4,91,2,8,96,9,54,6,10
131,2,7,101,4,5,107,3,104,7,9
141,2,3,4,101,6,2,3,87,4,95,6,9
151,2,3,9,51,6,2,7,47,8,55,7,8
161,2,8,4,51,6,3,101,2,7,101,2,7,10
171,2,8,91,6,4,91,2,8,91,2,8,9
181,7,3,4,51,6,8,51,4,5,101,3,6,10
191,7,3,91,7,3,4,51,6,3,101,3,7,9
201,7,8,41,7,3,91,6,4,91,4,5,10
211,4,5,101,7,8,41,6,8,51,4,6,9
221,9,101,9,101,7,3,91,4,7,8
236,2,7,52,3,5,101,7,8,41,5,6,8
246,2,3,4,52,4,9,52,3,5,102,3,5,10
256,2,3,92,7,3,82,4,9,52,3,6,9
266,2,8,42,8,102,7,3,82,3,7,8
276,7,3,43,8,4,53,8,4,52,4,5,9
286,4,103,8,96,2,3,92,4,6,8
296,9,56,2,3,4,56,2,7,52,5,6,7
302,7,3,86,2,3,96,2,8,43,4,5,8
312,3,5,106,2,7,56,7,3,43,4,6,7
322,8,106,2,8,41,2,3,4,101,2,3,4,10
332,4,9,56,4,101,2,3,9,51,2,3,5,9
347,3,106,7,3,41,2,8,4,51,2,3,6,8
357,8,56,9,51,6,2,3,81,2,4,5,8
367,4,97,3,101,6,2,7,41,2,4,6,7
373,8,4,57,4,91,7,3,4,51,3,4,5,7
383,8,97,8,56,2,3,4,52,3,4,5,6
Sheet1
Cell Formulas
RangeFormula
A8:A38A8=TRANSPOSE(FindSumCombinations(A1:B5,A7))
C8:C38C8=SORT(A8:A38)
Dynamic array formulas.
 
Upvote 0
What about having a help column, that calculates number of elements?
 
Upvote 0
What about having a help column, that calculates number of elements?
Thank you for the answer! However, how could help me your last indication, considering I need all elements to be shown clearly as numerical values, organized in specific groups? Can you show me the mentioned solution in a specific table / an example, with formula/s and distinctive groups as required? Thank you!
 
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