Nesting Linear

oliveiralexrui

New Member
Joined
Nov 30, 2024
Messages
6
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
Some time ago a vba code to perform linear nesting created by Erik was posted, I think. I would ask you to help me improve this code by including some rules, in order to create up to 1000 bars, put the minimum number of pieces per reference and up to 8 references per bar. The goal is to create as many equal bars as possible and work with the multiplicity of quantities per reference, so that when you finish a sequence of equal bars, you can also finish the maximum number of references. I would appreciate some help, thank you.
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
Give a bit more details about the source of "a vba code to perform linear nesting created by Erik was posted".
Link to such thread would be the best.
Otherwise probably only you and (if by chance notices this thread) Erik know what is the issue.
 
Upvote 0
The code is on this site.
htps://www.mrexcel.com/board/threads/l
inear-nesting.1230736/
 
Upvote 0
Hi, I think you're referring to a macro I wrote over a year ago. I'd have to dig in and figure out how it works, since I don't remember the specifics. It can handle 1000 bars as is though. What I don't understand is what you mean by "reference". So many pieces per reference, so many references per bar. The macro just figures out pieces per bar.
 
Upvote 0
I apologize for not expressing myself well. I'm Portuguese, I understand little English.

I work with countless pieces, each piece has a name that I call a reference. Each "name or part" reference can have several quantities.

What I would ask for, if possible, was to put less quantity per reference to put more references per bar, in order to replicate more of the same bars
 
Upvote 0
I'm afraid I still don't understand. Can you show an example of what your input data looks like, and what you'd like for the results?
 
Upvote 0
Captura de ecrã 2024-12-02 164458.png
 
Upvote 0
As far as I can tell, you want it to work the same, but group bars that are used the same way. If so, try this variation:

VBA Code:
Sub BarCut2()
Dim Rng As Range, sav As Variant, MyData As Variant, ctr(1 To 9999) As Long, bl As Double, ic As Double
Dim i As Long, j As Long, str1 As String, bool As Boolean, bn As Long, r As Long, barnum As Long
Dim bars As Variant, barqty As Long, itemqty As Long, dic As Object

    Set Rng = Range("A2:D" & Cells(Rows.Count, "A").End(xlUp).Row)
    sav = Rng.Value
        
    With ActiveSheet.Sort
        .SortFields.Clear
        .SortFields.Add2 Key:=Range("C2"), Order:=xlDescending
        .SetRange Rng
        .Header = xlNo
        .Apply
    End With
    
    MyData = Rng.Value
    Rng.Value = sav
    barnum = 1
    Range("H:I").ClearContents
    Set dic = CreateObject("Scripting.Dictionary")
    
    itemqty = WorksheetFunction.Sum(Range("B2:B1000"))
    barqty = WorksheetFunction.Sum(Range("E2:E1000"))
    ic = Range("D2").Value
    Range("H1").Value = "Results"
    Range("I1").Value = "Length used"
    
    bars = Range("E2:F" & Range("E999").End(xlUp).Row).Value
        
    Do While itemqty > 0 And barqty > 0
    
        For i = 1 To UBound(bars)
            If bars(i, 1) > 0 Then
                bl = bars(i, 2)
                Exit For
            End If
        Next i

        barused = 0
        Erase ctr
        bool = False
        Do While bl > 0
            For j = 1 To UBound(MyData)
                If MyData(j, 2) > 0 And MyData(j, 3) <= bl Then
                    ctr(j) = ctr(j) + 1
                    bl = bl - MyData(j, 3) - ic
                    barused = barused + MyData(j, 3) + ic
                    MyData(j, 2) = MyData(j, 2) - 1
                    bool = True
                    itemqty = itemqty - 1
                    Exit For
                End If
            Next j
            If j > UBound(MyData) Then Exit Do
        Loop
        
        If bool Then
            'str1 = "Bar " & barnum & ":  "
            
            For i = UBound(bars) To 1 Step -1
                If bars(i, 2) > barused - ic Then
                    bars(i, 1) = bars(i, 1) - 1
                    barqty = barqty - 1
                    Range("I100").End(xlUp).Offset(1).Value = bars(i, 2)
                    bl = bars(i, 2) - barused
                    str1 = "Bar " & bars(i, 2) & ": "
                    Exit For
                End If
            Next i
            
            For j = 1 To UBound(MyData)
                If ctr(j) > 0 Then str1 = str1 & ctr(j) & " X " & MyData(j, 1) & ", "
            Next j
            str1 = str1 & "Leftover: " & bl

            'Range("H100").End(xlUp).Offset(1).Value = str1
            dic(str1) = dic(str1) + 1
            barnum = barnum + 1
            
        End If
    
    Loop
            
    j = 2
    For Each x In dic
        Cells(j, "H") = dic(x) & " copies of: " & x
        j = j + 1
    Next x
    
    bool = False
    str1 = "Not made: "
    For i = 1 To UBound(MyData)
        If MyData(i, 2) > 0 Then
            bool = True
            str1 = str1 & MyData(i, 1) & " X " & MyData(i, 2) & ", "
        End If
    Next i
    If bool Then Cells(j + 2, "H") = Left(str1, Len(str1) - 2)
    
End Sub

Given the table below (columns A:F), you'll get the results in H:

Book1
ABCDEFGH
1NameQTYLENGTHINNER CUTBAR QTYBAR LENGTHRESULTSResults
2FS4T10819180200600036 copies of: Bar 6000: 3 X FS4T, Leftover: 246
3FS16T1081918100500036 copies of: Bar 6000: 3 X FS16T, Leftover: 246
4FS8T108166236 copies of: Bar 6000: 3 X FS8T, 1 X FS7TD, Leftover: 37
5FS19T108166218 copies of: Bar 6000: 3 X FS19T, 1 X FS7TD, Leftover: 37
6FS22T108166218 copies of: Bar 6000: 3 X FS19T, 1 X FS7TE, Leftover: 37
7FS21T108159736 copies of: Bar 6000: 3 X FS22T, 1 X FS7TE, Leftover: 37
8FS11T108144320 copies of: Bar 6000: 3 X FS21T, 1 X FS20TD, Leftover: 232
9FS3TD54134816 copies of: Bar 5000: 3 X FS21T, Leftover: 209
10FS3TE54134836 copies of: Bar 5000: 3 X FS11T, Leftover: 671
11FS17TD54134818 copies of: Bar 5000: 3 X FS3TD, Leftover: 956
12FS17TE54134818 copies of: Bar 5000: 3 X FS3TE, Leftover: 956
13FS7TD5497712 copies of: Bar 5000: 3 X FS17TD, Leftover: 956
14FS7TE54977
15FS20TD54977
16FS20TE54977Not made: FS17TD X 18, FS17TE X 54, FS20TD X 34, FS20TE X 54, FS12T X 108
17FS12T108972
Sheet1
 
Upvote 0
Hello Erik. Thank you for your time.

Part of the order is done, join the result of equal bars in a line.

But the objective, if possible, was to have more different pieces in bars, to obtain greater quantities of equal bars.

For example, instead of placing the same piece three times, placing one or two and placing different ones, this would generate a greater number of equal bars.
 
Upvote 0
I understand why that would be useful. But that is a very complicated problem. I stated in the previous thread that I used a "greedy" algorithm, which just tries to find the largest remaining piece that fits. It's very possible that 2 smaller pieces might fit, leaving less unused space. But that turns it into a combinatorics problem. Without even doing the math, I can tell you that there are millions of combinations of those bars. Read up on Knapsack Problem on Wikipedia. I actually have an idea on how to accomplish it, but it would be VERY slow. Possibly on the order of days to complete, or more. If I get a chance I might play around with it, but no promises.
 
Upvote 0

Forum statistics

Threads
1,225,750
Messages
6,186,808
Members
453,373
Latest member
Ereha

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