Formula to pick numbers to add to another number without going over 20,000

rplohocky

Active Member
Joined
Sep 25, 2005
Messages
292
Office Version
  1. 365
Platform
  1. Windows
Hello,
Is there a way for excel to look in a column at a list of numbers and then choose the ones that will add up to 20,000 without going over and then either mark or ignore the ones it has chosen then repeat until all the numbers in the column have been accounted for?

I have a column of numbers that like 600, 1000, 400, 5000 and so on, these numbers represent feet. I can order reels of wire up to 20,000 feet. I would then assign each of the numbers to be pulled from a specific reel.
Example: 600+2000+1500+8000+4000+400+3200=19,700 (this is close enough to 20k)
After this one excel would look at the remaining numbers in the column and repeat until they are all accounted for.

Thanks for any input!
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
@rplohocky
What is the scale of this in terms of typical max qty of numbers to allocate?
Can you accept a via solution?
 
Upvote 0
Snakehips, (cool name BTW)
The column would have maybe 100 rows max. What is a via solution?
 
Upvote 0
Sorry, via = auto-fill typo! Should read vba. Macro.

Also, how many reels might be typical for 100 rows ?
 
Upvote 0
Excel, however it can do it, should tell me how many reels of 20k and the left over size. So if it says i need two 20k reels but there is say 15,500 left then i would order 2 20k reels and 1 at 15,500.
 
Upvote 0
Do you have a preference for trying to allocate numbers in the list on a top down basis or can any number be allocated to any reel?
 
Upvote 0
@rplohocky Here is my best shot at this. Based on XL365, which you have.
It provides both manual and automatic possibilities for allocating wire lengths.

MrXL_Aug.xlsm
ABCDEFGHIJKLMNO
1
2
3Job RefWire LengthAllocated To Reel #Reel #Max Reel LengthAllocatedBalanceUn-Allocated JobsUn-Allocated LengthFind
4Job 16001120,00020,000000@
5Job 21004220,00019,97030Job RefUn-AllocatedRow
6Job 31504320,00019,9955  
7Job 42,0003415,0008,5806,420
8Job 51,5004520,000020,000
9Job 63504620,000020,000
10Job 74,0003720,000020,000
11Job 83704820,000020,000
12Job 94001920,000020,000
13Job 1039041020,000020,000
14Job 118,0002
15Job 123,2004
16Job 132004
17Job 142501
18Job 152503
19Job 162504
20Job 172504
21Job 182504
22Job 195,0003
23Job 204002
24Job 214002Max reel lengths in column G may be edited if required.
25Job 224004Populate / edit Job Ref and Wire Length data in columns B and C.
26Job 2318,7501For a totally fresh allocation, clear the reel numbers from D.
27Job 244004Otherwise, you can manually enter / edit Reel Numbers in D either
28Job 254004before, after, or instead of, doing an automatic allocation.
29Job 2611,1702Automatic allocation will only allocate rows not yet allocated.
30Job 273,2453Click the green button to auto-allocate.
31Job 285,5003
32Job 29804Best to have the sheet protected.
33Job 302904
34
35
36
37
38
Reel Calc
Cell Formulas
RangeFormula
H4:H13H4=SUMIF($D$4:$D$103,F4,$C$4:$C$103)
I4:I13I4=G4-H4
L4L4=COUNTIF(M6:M105,">0")
M4M4=SUM(M6:M102)
L6L6=IFERROR(SORT(FILTER(B4:C103,(C4:C103>0)*(D4:D103=""),),2,-1,),"")
N6:N105N6=IFERROR(MATCH(L6:L105,$B$4:$B$103,0)+3,"")
Dynamic array formulas.
Cells with Conditional Formatting
CellConditionCell FormatStop If True
G4:I13Expression=$H4 >$G4textNO
G4:I13Expression=$I4<=($G4*0.015)textNO
G4:I13Expression=AND($I4<=($G4*0.985),$I4>($G4*0.015))textNO


To do the Auto_Allocation, it will need the following code pasting to the code module of your sheet. Then the code will need to be assigned to the button shape.

VBA Code:
Sub Reel_Calc()
Dim r As Integer, Reel As Integer


'If nothing to allocate
If Range("L4") = 0 Then Exit Sub
'If there is an over-allocation for any reel
If WorksheetFunction.Min(Range("I4:I13")) < 0 Then
    MsgBox ("Please manually adjust existing allocatons to remove any over-allocation.   Then try again.")
    Exit Sub
End If
'Otherwise starting with reel 1
Reel = 1 '

'If reel cannot accomodate the smallest unallocated job then try next reel
Reel_Check:
If Range("I" & Reel + 3) < WorksheetFunction.Min(Range("M6:M105")) Then
    Reel = Reel + 1
    If Reel = 11 Then Exit Sub
    GoTo Reel_Check
End If

'If current reel will accomodate all remaining un-allocated jobs then allocate them all to current reel
If Range("I" & Reel + 3) >= Range("M4") Then
    Application.Calculation = xlCalculationManual
        For r = 6 To Range("L4") + 5
            Range("D" & Range("N" & r)) = Reel
        Next r
    Application.Calculation = xlCalculationAutomatic
End If

'Otherwise allocate where possible, numbers top down, largest to smallest, from un-allocated list

        For r = 6 To Range("L4") + 5
        If Range("M" & r) <= Range("I" & Reel + 3) Then
            Range("D" & Range("N" & r)) = Reel
            GoTo Reel_Check
        End If
        Next r
       
    Application.Calculation = xlCalculationAutomatic
End Sub

Hope that helps.

EDIT: Of course, the button doesn't render here etc.
I will endeavour to post a link to a working sheet.
 
Last edited:
Upvote 0
Snakehips,
I am blown away! I can't believe that you built something like this! I downloaded the file and found this to exactly what I was looking for. I can't thank you enough! I will incorporate this into my workbook and will always remember the work you did to make this happen!!!! Thanks again!
 
Upvote 0
Snakehips,
I am blown away! I can't believe that you built something like this! I downloaded the file and found this to exactly what I was looking for. I can't thank you enough! I will incorporate this into my workbook and will always remember the work you did to make this happen!!!! Thanks again!
That's fantastic to hear ! And you are most welcome.
There was never going to be a formula solution as such. So, it was a little challenge that kept me off the streets for a few hours.
Ultimately, very satisfying to know that it will be used and appreciated.
Anyway, if I hadn't been cooped up indoors, hunched over a computer, doing that for you, I'd probably have only been out enjoying myself! ;)
 
Upvote 0

Forum statistics

Threads
1,215,022
Messages
6,122,726
Members
449,093
Latest member
Mnur

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