combinations in excel

michaelsmith559

Well-known Member
Joined
Oct 6, 2013
Messages
881
Office Version
  1. 2013
  2. 2007
How would I calculate and list in excel the number of combinations that equal a given average? For example, how many ways can each of these values be used together to equal $75?
Values: $25, $30, $40, $50, $75, $100.
So obviously, $50 & $100 used 1 time each average $75.
How can I show how many combinations in total equal $75 & how can I list how many times the values are used together to equal that average? Thanks.
 

Excel Facts

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
Upvote 0
Solution
VBA Code:
Sub CountCombinations()

    Dim values() As Variant
    values = Array(25, 30, 40, 50, 75, 100)
    
    Dim target As Long
    target = 75
    
    Dim combos() As Variant
    ReDim combos(0 To 5, 0 To 2)
    combos(0, 0) = 25
    combos(1, 0) = 30
    combos(2, 0) = 40
    combos(3, 0) = 50
    combos(4, 0) = 75
    combos(5, 0) = 100
    
    Dim numCombos As Long
    numCombos = 0
    
    For i = 0 To 5
        For j = i To 5
            For k = j To 5
                For l = k To 5
                    For m = l To 5
                        For n = m To 5
                            If combos(i, 0) + combos(j, 0) + combos(k, 0) + combos(l, 0) + combos(m, 0) + combos(n, 0) = target Then
                                numCombos = numCombos + 1
                                combos(i, 1) = combos(i, 1) + 1
                                combos(j, 1) = combos(j, 1) + 1
                                combos(k, 1) = combos(k, 1) + 1
                                combos(l, 1) = combos(l, 1) + 1
                                combos(m, 1) = combos(m, 1) + 1
                                combos(n, 1) = combos(n, 1) + 1
                            End If
                        Next n
                    Next m
                Next l
            Next k
        Next j
    Next i
    
    Dim output() As Variant
    ReDim output(0 To 6, 0 To 1)
    output(0, 0) = "Value"
    output(0, 1) = "Count"
    
    For i = 0 To 5
        output(i + 1, 0) = combos(i, 0)
        output(i + 1, 1) = combos(i, 1)
    Next
    
    output(7, 0) = "Total Combinations"
    output(7, 1) = numCombos
    
    Range("A1:B8") = output
    
End Sub
This code uses nested loops to iterate through all possible combinations of the given values and checks whether each combination adds up to the target value. If a combination is found, the code increments the count of each value used in that combination.

The code then outputs the results to a new worksheet, with each row showing a value and the count of how many times it was used in combinations that add up to the target value. The last row shows the total number of combinations that add up to the target value.

To use this code, simply copy and paste it into a new module in your Excel workbook and run the CountCombinations subroutine. The results will be output to a new worksheet in the same workbook.
 
Upvote 0
VBA Code:
Sub CountCombinations()

    Dim values() As Variant
    values = Array(25, 30, 40, 50, 75, 100)
   
    Dim target As Long
    target = 75
   
    Dim combos() As Variant
    ReDim combos(0 To 5, 0 To 2)
    combos(0, 0) = 25
    combos(1, 0) = 30
    combos(2, 0) = 40
    combos(3, 0) = 50
    combos(4, 0) = 75
    combos(5, 0) = 100
   
    Dim numCombos As Long
    numCombos = 0
   
    For i = 0 To 5
        For j = i To 5
            For k = j To 5
                For l = k To 5
                    For m = l To 5
                        For n = m To 5
                            If combos(i, 0) + combos(j, 0) + combos(k, 0) + combos(l, 0) + combos(m, 0) + combos(n, 0) = target Then
                                numCombos = numCombos + 1
                                combos(i, 1) = combos(i, 1) + 1
                                combos(j, 1) = combos(j, 1) + 1
                                combos(k, 1) = combos(k, 1) + 1
                                combos(l, 1) = combos(l, 1) + 1
                                combos(m, 1) = combos(m, 1) + 1
                                combos(n, 1) = combos(n, 1) + 1
                            End If
                        Next n
                    Next m
                Next l
            Next k
        Next j
    Next i
   
    Dim output() As Variant
    ReDim output(0 To 6, 0 To 1)
    output(0, 0) = "Value"
    output(0, 1) = "Count"
   
    For i = 0 To 5
        output(i + 1, 0) = combos(i, 0)
        output(i + 1, 1) = combos(i, 1)
    Next
   
    output(7, 0) = "Total Combinations"
    output(7, 1) = numCombos
   
    Range("A1:B8") = output
   
End Sub
This code uses nested loops to iterate through all possible combinations of the given values and checks whether each combination adds up to the target value. If a combination is found, the code increments the count of each value used in that combination.

The code then outputs the results to a new worksheet, with each row showing a value and the count of how many times it was used in combinations that add up to the target value. The last row shows the total number of combinations that add up to the target value.

To use this code, simply copy and paste it into a new module in your Excel workbook and run the CountCombinations subroutine. The results will be output to a new worksheet in the same workbook.
Tried running the code but it gives subscript out of range and highlights this in the code:
output(7, 0) = "Total Combinations"
 
Upvote 0
Let's start here to see if we get anywhere.

[Please amend your profile signature so that it includes the version of excel you're using.]

Cell Formulas
RangeFormula
E5:J10E5=AVERAGE($D5,E$4)
Cells with Conditional Formatting
CellConditionCell FormatStop If True
E5:J10Cell Value=$D$2textNO
Dr Steele,
I thought about doing the same thing you did, kind of like rolling two six-sided dice and seeing what the values add up to. However, what if 20+30+100 occurs? I'm not sure how to account combinations like this. Anything else you can think of?
 
Upvote 0
Dr Steele,
I thought about doing the same thing you did, kind of like rolling two six-sided dice and seeing what the values add up to. However, what if 20+30+100 occurs? I'm not sure how to account combinations like this. Anything else you can think of?
Or does the way you did it account for all combinations that would come to that average?
 
Upvote 0
Or does the way you did it account for all combinations that would come to that average?
Let me ask this a different way.
If 25 can occur 6548 times, 30 occur 2856 times, 40 occur 3045 times, 50 occur 2938 times, 75 occur 569 times, and 100 occur 606 times; how many ways can these values combine to average out to 75?
 
Upvote 0
Is this the kind of result you are expecting:
Book1
ABC
12575 Average: 75
23050-100 Average: 75
34050-75-100 Average: 75
450
575
6100
Sheet1
 
Upvote 0
This may be useless to you if you're working with Excel 2019 or older, but if not, this might help.
Shitty.xlsx
ABCDEFGHI
1# of Items: 20ItemCountIncrement: 5
2Max #: 100Count between 0 and 4: 004
328Count between 5 and 9: 059
468Count between 10 and 14: 11014
517Count between 15 and 19: 31519
640Count between 20 and 24: 22024
785Count between 25 and 29: 32529
834Count between 30 and 34: 13034
919Count between 35 and 39: 03539
1045Count between 40 and 44: 14044
1122Count between 45 and 49: 14549
1217Count between 50 and 54: 15054
1326Count between 55 and 59: 15559
1464Count between 60 and 64: 16064
1554Count between 65 and 69: 26569
1624Count between 70 and 74: 17074
1726Count between 75 and 79: 07579
1885Count between 80 and 84: 08084
1957Count between 85 and 89: 28589
2070Count between 90 and 94: 09094
2111Count between 95 and 99: 09599
2265
Sheet1
Cell Formulas
RangeFormula
D2:D21D2="Count between "&H2#&" and "&I2#&": "
E2:E21E2=COUNTIFS(A3#,">="&H2#,A3#,"<="&I2#)
H2:H21H2=SEQUENCE(B2/H1,,0,H1)
I2:I21I2=H2#+$H$1-1
A3:A22A3=RANDARRAY(B1,,0,B2,1)
Dynamic array formulas.
Cells with Data Validation
CellAllowCriteria
H1Whole numberbetween 2 and B1
 
Upvote 0
Dr Steele,
I thought about doing the same thing you did, kind of like rolling two six-sided dice and seeing what the values add up to. However, what if 20+30+100 occurs? I'm not sure how to account combinations like this. Anything else you can think of?

I misunderstood the problem you presented. Sorry 'bout dat.
 
Upvote 0

Forum statistics

Threads
1,215,219
Messages
6,123,684
Members
449,116
Latest member
HypnoFant

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