Five Values that Equal a Sum Value

ststern45

Well-known Member
Joined
Sep 17, 2005
Messages
985
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi Everyone,
Happy New Year 2025

I have a set of 30 values below:
There are duplicate values in the list below. Example: 1, 2, 3 etc.
The minimum value total is 5 (1,1,1,1,1) and the maximum equal 73 (10,11,12,16,24)
So the range is from 5 to 73.

I would like to calculate how many 5 number values would equal sum of 5, 6, 7, .... 73

Thank you!!

1
1
1
1
1
2
2
2
2
3
3
3
3
4
4
5
5
5
5
5
6
6
7
8
8
10
11
12
16
24
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Hi ststern45. So after you get the 5 lowest numbers summed and your 5 highest numbers summed and from that you have a new range of numbers (ie. 5 to 73) how do you display the 5 number combinations from Column "A" that equal your numbers in your new range? Also, why would you want/need to do this? Dave
 
Upvote 0
I found the following code which works for finding the sum of 1 number such as 5 or 73.
The hard part is finding the sums from 6 through 72.
In addition, having a list that has duplicate values such as 1's, 2's, 3's etc.

This is the code from ablebits.

This is how it should look below: Sum Values to the left and total sets per sum value on the right.
I used a longer method to come up with the values below but looking for a quicker/easier way to generate the sets.
Thank you.

Sums Total Sets
5 1
6 0
7 2
8 3
9 5
10 9
11 12
12 18
13 30
14 40
15 55
16 71
17 96
18 128
19 164
20 211
21 241
22 303
23 358
24 443
25 496
26 603
27 693
28 827
29 924
30 1065
31 1163
32 1321
33 1456
34 1628
35 1762
36 1938
37 2115
38 2302
39 2464
40 2641
41 2826
42 2979
43 3136
44 3270
45 3472
46 3676
47 3966
48 4245
49 4602
50 4920
51 5299
52 5532
53 5793
54 5903
55 6065
56 6067
57 6025
58 5802
59 5610
60 5237
61 4813
62 4257
63 3674
64 3007
65 2356
66 1756
67 1179
68 730
69 410
70 210
71 80
72 20
73 1

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
 
Upvote 0
Maybe I'm misunderstanding, the total set are each 1 group of 5 different numbers from "A" that sum to the Sums number. + 6000 different combinations/ number sets are available in "A" to make your Sums 55 to 57? Seems incredible. How did you get the total sets? Do you not want to display the number sets somewhere? Dave
 
Upvote 0
There a total of 30 numbers from 1 through 30 that each have their own unique value.
This list below is sorted in ascending order from 1 through 30
Left side are the numbers from 1 through 30 and the right side with the values pertaining to each of the numbers 1 through 30
# Value
1 12
2 7
3 1
4 9
5 8
6 5
7 7
8 4
9 6
10 1
11 17
12 4
13 25
14 4
15 6
16 4
17 3
18 1
19 3
20 13
21 11
22 1
23 9
24 6
25 6
26 1
27 6
28 3
29 3
30 5
 
Upvote 0
This is the same list with the values on the left sorted from low to high:
# Value
26 1
22 1
18 1
10 1
3 1
29 3
28 3
19 3
17 3
16 4
14 4
12 4
8 4
30 5
6 5
27 6
25 6
24 6
15 6
9 6
7 7
2 7
5 8
23 9
4 9
21 11
1 12
20 13
11 17
13 25
 
Upvote 0
This was the chart posted in the opening post:
If you total the values on the right it would total 142,506
Sums Totals
5 1
6 0
7 2
8 3
9 5
10 9
11 12
12 18
13 30
14 40
15 55
16 71
17 96
18 128
19 164
20 211
21 241
22 303
23 358
24 443
25 496
26 603
27 693
28 827
29 924
30 1065
31 1163
32 1321
33 1456
34 1628
35 1762
36 1938
37 2115
38 2302
39 2464
40 2641
41 2826
42 2979
43 3136
44 3270
45 3472
46 3676
47 3966
48 4245
49 4602
50 4920
51 5299
52 5532
53 5793
54 5903
55 6065
56 6067
57 6025
58 5802
59 5610
60 5237
61 4813
62 4257
63 3674
64 3007
65 2356
66 1756
67 1179
68 730
69 410
70 210
71 80
72 20
73 1
72 20
73 1
 
Last edited:
Upvote 0
The "long" method I used was to create all 5 number sets in Excel from 1 through 30 which totals 142,506. =combin(30,5)
1>Each 5 number set starting with 1, 2, 3, 4, 5 would also have the values that match. So in this example 1,2,3,4,5 the values would be 12, 7, 1, 9, 8 = sum total 37
2>This continues until the last set of 26, 27, 28, 29, 30 and their matching values (see list from the last few posts. 1, 6, 3, 3, 5 = sum total 18
3>The "long" calculations tally all 142,506 five numbers sets and their sum totals then count the totals for each sum totals from 5 though 73
4>Using the previous list, the sum value 37 occurred 2115 times and the sum total 18 occurred 128 times.
 
Upvote 0
This is the code below used to create all 142,506 sets.
Once all 142,506 sets are created I just match the values on the right from the list below to the values 1 through 30.
# Value
1 12
2 7
3 1
4 9
5 8
6 5
7 7
8 4
9 6
10 1
11 17
12 4
13 25
14 4
15 6
16 4
17 3
18 1
19 3
20 13
21 11
22 1
23 9
24 6
25 6
26 1
27 6
28 3
29 3
30 5


Option Explicit
Dim A As Integer, B As Integer, C As Integer, D As Integer, E As Integer
Dim N As Long, nMinA As Integer, nMaxE As Integer

Sub List_5x30Comb()

' Macro to list combinations in an Excel sheet
Sheets("530Sets").Select
Range("A1").Select
Application.ScreenUpdating = False
N = 1
Selection.ColumnWidth = 10
ActiveCell.Value = "Combinations"

' Change minimum value for A or maximum value for F
' to cover range of values to be covered
nMinA = 1
nMaxE = 30

' Start of loops for 5 variables (A-E)
For A = nMinA To nMaxE - 4
For B = A + 1 To nMaxE - 3
For C = B + 1 To nMaxE - 2
For D = C + 1 To nMaxE - 1
For E = D + 1 To nMaxE


' Copying the combination in the form N1-N2-N3-N4-N5-N6 in a cell
ActiveCell.Offset(1, 0).Select
N = N + 1
ActiveCell.Value = Application.WorksheetFunction.Text(A, "00") & "," _
& Application.WorksheetFunction.Text(B, "00") & "," _
& Application.WorksheetFunction.Text(C, "00") & "," _
& Application.WorksheetFunction.Text(D, "00") & "," _
& Application.WorksheetFunction.Text(E, "00")


Next E
Next D
Next C
Next B
Next A
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Here's one way to generate the full set. The results match yours in Post #7, but yours are upside down.

ABC
1NumbersResults
2151
31620
41780
518210
619410
7210730
82111,179
92121,756
102132,356
113143,007
123153,674
133164,257
143174,813
154185,237
164195,610
175205,802
185216,025
195226,067
205236,065
215245,903
226255,793
236265,532
247275,299
258284,920
268294,602
2710304,245
2811313,966
2912323,676
3016333,472
3124343,270
32353,136
33362,979
34372,826
35382,641
36392,464
37402,302
38412,115
39421,938
40431,762
41441,628
42451,456
43461,321
44471,163
45481,065
4649924
4750827
4851693
4952603
5053496
5154443
5255358
5356303
5457241
5558211
5659164
5760128
586196
596271
606355
616440
626530
636618
646712
65689
66695
67703
68712
69720
70731
Sheet1

VBA Code:
Sub Test()

    Dim MyNumbers As Variant
    Dim MyIntegers() As Long, MyCombinations() As Long, MySum As Long, i As Long, j As Long, k As Long, N As Long, MyMin As Long, MyMax As Long, Results() As Long
    
    MyNumbers = Range("A2:A31").Value
    N = UBound(MyNumbers)
    k = 5
    'Convert to Long for speed
    ReDim MyIntegers(1 To N)
    For i = 1 To N
        MyIntegers(i) = MyNumbers(i, 1)
    Next i
    
    MyCombinations = GetCombinations(N, k)
    
    'Assumes MyIntegers are in ascending order
    For i = 1 To k
        MyMin = MyMin + MyIntegers(i)
        MyMax = MyMax + MyIntegers(N - i + 1)
    Next i
    ReDim Results(MyMin To MyMax, 1 To 2)
    For i = MyMin To MyMax
        Results(i, 1) = i
    Next i
        
    For i = 1 To UBound(MyCombinations)
        For j = 1 To k
            MySum = MySum + MyIntegers(MyCombinations(i, j))
        Next j
        Results(MySum, 2) = Results(MySum, 2) + 1
        MySum = 0
    Next i

    Range("B2").Resize(UBound(Results) - LBound(Results) + 1, UBound(Results, 2)).Value = Results

End Sub
Function GetCombinations(lNumber As Long, lNoChosen As Long) As Long()

    Dim lOutput() As Long, lCombinations As Long
    Dim i As Long, j As Long, k As Long
    
    lCombinations = WorksheetFunction.Combin(lNumber, lNoChosen)
    ReDim lOutput(1 To lCombinations, 1 To lNoChosen)
    
    For i = 1 To lNoChosen
        lOutput(1, i) = i
    Next i
    
    For i = 2 To lCombinations
        For j = 1 To lNoChosen
            lOutput(i, j) = lOutput(i - 1, j)
        Next j
        For j = lNoChosen To 1 Step -1
            lOutput(i, j) = lOutput(i, j) + 1
            If lOutput(i, j) <= lNumber - (lNoChosen - j) Then Exit For
        Next j
        For k = j + 1 To lNoChosen
            lOutput(i, k) = lOutput(i, k - 1) + 1
        Next k
    Next i
    
    GetCombinations = lOutput
    
End Function
 
Upvote 0
Solution

Forum statistics

Threads
1,225,741
Messages
6,186,763
Members
453,370
Latest member
juliewar

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