Ordering Exercise

tiredofit

Well-known Member
Joined
Apr 11, 2013
Messages
1,926
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
This is my data:

1718821509617.png


and these are the results:

931
935
934
936
915
914
916
954
956
3154
3156
3156
3546
1546
 
Last edited:

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Why is 5,4,9 invalid? 5,4 is the last largest pair.
 
Upvote 0
1,5,4,6 is equal to 50. Should be excluded. Try:
loop50.xlsm
ABC
19309,3,1
23209,3,5
31159,3,4
45159,3,6
54109,1,5
66109,1,4
729,1,6
879,5,4
989,5,6
10103,1,5,4
113,1,5,6
123,1,4,6
133,5,4,6
Sheet1


VBA Code:
Sub FindSumCombinations()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change sheet name if needed
    
    Dim colA As Range
    Dim colB As Range
    Dim lastRow As Long
    Dim i As Long, j As Long, k As Long, l As Long
    Dim sum As Long
    Dim threshhold As Double
    Dim results As Collection
    
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    Set colA = ws.Range("A1:A" & lastRow)
    Set colB = ws.Range("B1:B" & lastRow)
    Set results = New Collection
    threshold = 50
    
    For i = 1 To lastRow - 1
        For j = i + 1 To lastRow
            sum = colB(i).Value + colB(j).Value
            If sum > threshold Then
                results.Add colA(i).Value & "," & colA(j).Value
            Else
                For k = j + 1 To lastRow
                    sum = colB(i).Value + colB(j).Value + colB(k).Value
                    If sum > threshold Then
                        results.Add colA(i).Value & "," & colA(j).Value & "," & colA(k).Value
                    Else
                        For l = k + 1 To lastRow
                            sum = colB(i).Value + colB(j).Value + colB(k).Value + colB(l).Value
                            If sum > threshold Then
                                results.Add colA(i).Value & "," & colA(j).Value & "," & colA(k).Value & "," & colA(l).Value
                            End If
                        Next l
                    End If
                Next k
            End If
        Next j
    Next i
    
    ' Output results
    Dim outputRow As Long
    outputRow = 1
    For Each result In results
        ws.Cells(outputRow, "C").Value = result
        outputRow = outputRow + 1
    Next result
End Sub
 
Last edited:
Upvote 0
Solution
1,5,4,6 is equal to 50. Should be excluded. Try:
loop50.xlsm
ABC
19309,3,1
23209,3,5
31159,3,4
45159,3,6
54109,1,5
66109,1,4
729,1,6
879,5,4
989,5,6
10103,1,5,4
113,1,5,6
123,1,4,6
133,5,4,6
Sheet1


VBA Code:
Sub FindSumCombinations()
    Dim ws As Worksheet
    Set ws = ThisWorkbook.Sheets("Sheet1") ' Change sheet name if needed
   
    Dim colA As Range
    Dim colB As Range
    Dim lastRow As Long
    Dim i As Long, j As Long, k As Long, l As Long
    Dim sum As Long
    Dim threshhold As Double
    Dim results As Collection
   
    lastRow = ws.Cells(ws.Rows.Count, "B").End(xlUp).Row
    Set colA = ws.Range("A1:A" & lastRow)
    Set colB = ws.Range("B1:B" & lastRow)
    Set results = New Collection
    threshold = 50
   
    For i = 1 To lastRow - 1
        For j = i + 1 To lastRow
            sum = colB(i).Value + colB(j).Value
            If sum > threshold Then
                results.Add colA(i).Value & "," & colA(j).Value
            Else
                For k = j + 1 To lastRow
                    sum = colB(i).Value + colB(j).Value + colB(k).Value
                    If sum > threshold Then
                        results.Add colA(i).Value & "," & colA(j).Value & "," & colA(k).Value
                    Else
                        For l = k + 1 To lastRow
                            sum = colB(i).Value + colB(j).Value + colB(k).Value + colB(l).Value
                            If sum > threshold Then
                                results.Add colA(i).Value & "," & colA(j).Value & "," & colA(k).Value & "," & colA(l).Value
                            End If
                        Next l
                    End If
                Next k
            End If
        Next j
    Next i
   
    ' Output results
    Dim outputRow As Long
    outputRow = 1
    For Each result In results
        ws.Cells(outputRow, "C").Value = result
        outputRow = outputRow + 1
    Next result
End Sub
I've tested your code and it works as expected.

Apologies for the mistake 43 1,5,4,6 (and you prob as spotted 3,1,5,6 was typed twice).

It took me A VERY LONG time just to formulate an easier structure (sorting from larhgest to smallest).

Many thanks for your help over the last few days.
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,182
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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