Find any nth values from list that can sumup to a value not greater than that cell

shersalafi

New Member
Joined
Dec 24, 2021
Messages
4
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
Hi, to every one.
I am creating a report. i have a list of different numbers in range M4:M21
What i need is to sum up any 3 or 4 values that can near to 240 but not more than that
For Example I have This List
Range M4 to M21
57
81
87
50
85
66
70
72
81
54
82
59
86
53
69
84
63
83

Now i need Any Combination of 3 values or 4 values which sums the maximum number but should be less than 240.
Like in Above List 86 + 84 + 69 = 239
Then 63 + 82 + 87 = 232
It should give me Like That but not more than 240
and the cells which are used should not be used again like Cell M16 (i.e. 86) , M19, M18 is used in one combination so it should not be used again.
And 3 or four values which is used should be coloured differently.
if there is any macro you can suggest
Thanks for you support in advance.
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Hi,

I created VBA to generate 5 combinations.

Please check.

1. All numbers are in column A and sorted from Highest to Lowest.
2. Column B contains "N" for each number to show that the number is used or not.
3. Column F, G, H and I contains the combination and the sum.

VBA Code:
Sub createCombination()
Dim num1 As Integer, num2 As Integer, num3 As Integer
Dim n1 As Integer, n2 As Integer, n3 As Integer
Dim totalNum As Integer, rownum As Integer, total As Integer
totalNum = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Const sumReqd As Integer = 240

Application.ScreenUpdating = False
Restart:
For n1 = 1 To totalNum
    If Range("B" & n1) = "N" Then
        num1 = Range("A" & n1)
        For n2 = n1 + 1 To totalNum
            If Range("B" & n2) = "N" Then
                num2 = Range("A" & n2)
                For n3 = n2 + 1 To totalNum
                    If Range("B" & n3) = "N" Then
                        num3 = Range("A" & n3)
                        total = num1 + num2 + num3
                        If total < sumReqd Then
                            Range("B" & n1) = "Y"
                            Range("B" & n2) = "Y"
                            Range("B" & n3) = "Y"
                            rownum = ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Row + 1
                            Range("F" & rownum) = num1
                            Range("G" & rownum) = num2
                            Range("H" & rownum) = num3
                            Range("I" & rownum) = total
                            GoTo Restart
                        End If
                    End If
                Next
            End If
        Next
    End If
Next
Application.ScreenUpdating = True
End Sub

NumberCombinations.xlsm
ABCDEFGHIJK
187Ynum1num2num3total
286Y878666239
385Y858470239
484Y838272237
583Y818169231
682Y635957179
781Y545350157
881Y
972Y
1070Y
1169Y
1266Y
1363Y
1459Y
1557Y
1654Y
1753Y
1850Y
19
20
21
22
23
24
25
26
Sheet1
 
Upvote 0
Hi,

I created VBA to generate 5 combinations.

Please check.

1. All numbers are in column A and sorted from Highest to Lowest.
2. Column B contains "N" for each number to show that the number is used or not.
3. Column F, G, H and I contains the combination and the sum.

VBA Code:
Sub createCombination()
Dim num1 As Integer, num2 As Integer, num3 As Integer
Dim n1 As Integer, n2 As Integer, n3 As Integer
Dim totalNum As Integer, rownum As Integer, total As Integer
totalNum = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row
Const sumReqd As Integer = 240

Application.ScreenUpdating = False
Restart:
For n1 = 1 To totalNum
    If Range("B" & n1) = "N" Then
        num1 = Range("A" & n1)
        For n2 = n1 + 1 To totalNum
            If Range("B" & n2) = "N" Then
                num2 = Range("A" & n2)
                For n3 = n2 + 1 To totalNum
                    If Range("B" & n3) = "N" Then
                        num3 = Range("A" & n3)
                        total = num1 + num2 + num3
                        If total < sumReqd Then
                            Range("B" & n1) = "Y"
                            Range("B" & n2) = "Y"
                            Range("B" & n3) = "Y"
                            rownum = ActiveSheet.Cells(Rows.Count, "F").End(xlUp).Row + 1
                            Range("F" & rownum) = num1
                            Range("G" & rownum) = num2
                            Range("H" & rownum) = num3
                            Range("I" & rownum) = total
                            GoTo Restart
                        End If
                    End If
                Next
            End If
        Next
    End If
Next
Application.ScreenUpdating = True
End Sub

NumberCombinations.xlsm
ABCDEFGHIJK
187Ynum1num2num3total
286Y878666239
385Y858470239
484Y838272237
583Y818169231
682Y635957179
781Y545350157
881Y
972Y
1070Y
1169Y
1266Y
1363Y
1459Y
1557Y
1654Y
1753Y
1850Y
19
20
21
22
23
24
25
26
Sheet1
Thanks A lot

Saurabhj Thank you so much for your kind support. Its worked for me. now having an issue that i want to cut a piece of wood which length is 240 centimetres so according to your screenshot first Four Totals are good i.e. I2 to I5 in which we have remaining less amount of Scrap so our wood pieces will not be go in trash (less loss in lengths when cutting wood pieces) but i want to add another cell also like See I6 cell, its total is 179 so we can also add F7(value 54) to make it 233. so from 240CM of wood only 7CM will go in the dustbin and 233 will be used. see image below​

 

Attachments

  • values.png
    values.png
    20.2 KB · Views: 23
Upvote 0
I tried it myself and edit this macro but not working because now it sums up only 4 values and leaves others. I need to make it less than 240 whether it could be by summing two values or three or four.
 
Upvote 0
Welcome to the MrExcel Message Board!

Cross-posting (posting the same question in more than one forum) is not against our rules, but the method of doing so is covered by #13 of the Forum Rules.

Be sure to follow & read the link at the end of the rule too!

Cross posted at: How To Find Maximum Sum From The List With nth Number of Values Not Greater Than To X
If you have posted the question at more places, please provide links to those as well.

If you do cross-post in the future and also provide links, then there shouldn’t be a problem.
 
Upvote 0
Sorry it was my first time, Yes I will mention there your link also, and there on that forum link is this one
 
Upvote 0

Forum statistics

Threads
1,223,950
Messages
6,175,582
Members
452,653
Latest member
craigje92

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