Combinations of multiple products & quantities such total amount matchs given number

bebo021999

Well-known Member
Joined
Jul 14, 2011
Messages
2,541
Office Version
  1. 2016
I have a list of products and their prices in the range "A:D". I have a total amount, for example: 5,000,000.
The total amount is exact, with no allowance for even a 1 cent difference. I need to create an invoice with a maximum of 10 products randomly (with its quantity and unit price, picked from the product list)
and Total amount matching the specified number.
This invoice can have 1, 2, 3, or up to 10 products. Please provide a VBA code snippet to list all possible invoices that can be created.
In the file, the range H3:L14 is an example of an invoice that can be generated.
Thanks in advance!

link of sharing file

crossed-post here
 

Excel Facts

Excel motto
Not everything I do at work revolves around Excel. Only the fun parts.
Please have a look at an earlier thread where a similar solution was sought. This falls in the area of combinatorics, and the objective then was to find some combination of items whose sum is closest to a target value. You may find something that will work or that can be adapted. Also have a look at offerings from @Xlambda, who documented some LAMBDA functions involving combinatorics.

 
Upvote 1
Hello, I am here because @KRice mention me and I kindly thank him for that 🙏. So, allow me to propose a solution.
Wrote a recursive function that does the job for a certain chosen number of products "np". Define name "up" - the array of unit prices up: DATA!$D$3:$D$830
PL(np
) Product List
The concept is very simple. We select random np-1 products and a random number of units (estimated proportionally with averages) that produce a total. For the rest of the products, we look for a match of a certain product whose unit price multiplied by an array of pcs produces the exact 5000000-random total.
The function does all the cosmetics needed; the conceptual part is short. Also is fully dynamic, 5,000,000 constant can be changed (f variable) and np is not limited to 10, also the product list can be bigger or shorter.
Let me know if it's what you need. The number of total possibilities is astronomic. Also, if you need to freeze these functions to stop recalculating, I have a function for that. hope you like it. ✌️
Excel Formula:
=LAMBDA(np,
    LET(
        f, 5000000,
        t, "Product ",
        o, "0000",
        ta, {"No", "Product", "Quantity", "U.P", "Amount"},
        tp, ROWS(up),
        st, SEQUENCE(tp),
        av, AVERAGE(up),
        n, np - 1,
        e, SEQUENCE( n),
        m, INT(f / av / np) + 15,
        c, SEQUENCE(, m),
        x, INDEX(SORTBY(st, RANDARRAY(tp)), e),
        y, RANDARRAY(n, , 1, m, 1),
        u, INDEX(up, x),
        z, u * y,
        s, f - SUM(z),
        r, FILTER(st, ISNA(XMATCH(st, x))),
        q, SEQUENCE(ROWS(r), m),
        a, INDEX(up, r) * c,
        b, XMATCH(a, s),
        i, AND(ISNA(b)),
        IF(
            i,
            PL(np),
            LET(
                j, MAX(ISNUMBER(b) * q),
                w, QUOTIENT(j - 1, m) + 1,
                l, MOD(j - 1, m) + 1,
                p, INDEX(r, w),
                v, INDEX(up, p),
                k, VSTACK(HSTACK(e, t & TEXT(x, o), y, u, z), HSTACK(np, t & TEXT(p, o), l, v, l * v)),
                VSTACK(ta, k, HSTACK("", "TOTAL", "", "", SUM(TAKE(k, , -1))))
            )
        )
    )
)
Invoice.xlsm
ABCDEFGHIJKLMNOPQRS
1TABLE 1expected result (manually input)
2NoProductUnitUnit priceRandomlyRandomly=PL(7)
31Product 0001pcs32,500NoProductQuantityU.PAmountNoProductQuantityU.PAmount
42Product 0002pcs33,5001Product 00262033,500670,0001Product 072433330001089000
53Product 0003pcs32,0002Product 00425013,300665,0002Product 05243222600723200
64Product 0004pcs33,0003Product 00491532,500487,5003Product 01532923300675700
75Product 0005pcs67,0004Product 01883022,600678,0004Product 01362240200884400
86Product 0006pcs34,0005Product 03222532,100802,5005Product 065722330046600
97Product 0007pcs55,0006Product 04552011,500230,0006Product 0343555000275000
108Product 0008pcs73,5007Product 0655855,000440,0007Product 076137353001306100
119Product 0009pcs23,3008Product 01121540,200603,000TOTAL5000000
1210Product 0010pcs32,1009Product 05561133,000363,000
1311Product 0011pcs40,30010Product 0013161,00061,000=PL(8)
1412Product 0012pcs25,900TOTAL5,000,000NoProductQuantityU.PAmount
1513Product 0013pcs61,000 test 5,000,000 OK 1 Product 0676 1733,000561,000
1614Product 0014pcs29,1002 Product 0825 2344,0001,012,000
1715Product 0015pcs33,400=PL(3)3Product 0819737500262500
1816Product 0016pcs40,200NoProductQuantityU.PAmount4Product 0649732500227500
1917Product 0017pcs35,3001Product 0533626700041540005Product 060817735001249500
2018Product 0018pcs13,3002Product 014352115005980006Product 017319670001273000
2119Product 0019pcs17,3003Product 08138310002480007Product 00233311500379500
2220Product 0020pcs22,600TOTAL50000008Product 07897500035000
2321Product 0021pcs20,400TOTAL5000000
2422Product 0022pcs29,300=PL(4)
2523Product 0023pcs11,500NoProductQuantityU.PAmount=PL(9)
2624Product 0024pcs33,7001Product 063645259001165500NoProductQuantityU.PAmount
2725Product 0025pcs32,5002Product 0296167350011760001Product 07471432000448000
2826Product 0026pcs33,5003Product 0536337350024255002Product 044017350073500
2927Product 0027pcs32,0004Product 075310233002330003Product 00782134000714000
3028Product 0028pcs33,000TOTAL50000004Product 06592240300886600
3129Product 0029pcs67,0005Product 064632930087900
3230Product 0030pcs34,000=PL(5)6Product 003031340001054000
3331Product 0031pcs55,000NoProductQuantityU.PAmount7Product 04381334000442000
3432Product 0032pcs73,5001Product 0007445500024200008Product 02551233400400800
3533Product 0033pcs23,3002Product 05109340003060009Product 07572930800893200
3634Product 0034pcs32,1003Product 01121540200603000TOTAL5000000
3735Product 0035pcs40,3004Product 07482433000792000
3836Product 0036pcs25,9005Product 07663029300879000=PL(10)
3937Product 0037pcs30,800TOTAL5000000NoProductQuantityU.PAmount
4038Product 0038pcs29,1001Product 02171732500552500
4139Product 0039pcs33,400=PL(6)2Product 06822132100674100
4240Product 0040pcs40,200NoProductQuantityU.PAmount3Product 03941832100577800
4341Product 0041pcs35,3001Product 074512325003900004Product 07701415600218400
4442Product 0042pcs13,3002Product 072328320008960005Product 06032432000768000
4543Product 0043pcs17,3003Product 0347374030014911006Product 0728873500588000
4644Product 0044pcs22,6004Product 0688364020014472007Product 0511655000330000
4745Product 0045pcs20,4005Product 061223259005957008Product 07951211500138000
4846Product 0046pcs29,3006Product 080110180001800009Product 06291267000804000
4947Product 0047pcs11,500TOTAL500000010Product 07581229100349200
5048Product 0048pcs33,700TOTAL5000000
5149Product 0049pcs32,500
5250Product 0050pcs33,500
DATA
Cell Formulas
RangeFormula
B3:B52B3="Product " & TEXT(A3,"0000")
N2,H41,N38,H32,N25,H24,H17,N13N2=FORMULATEXT(N3)
N3:R11N3=PL(7)
A4:A52,H5:H13A4=A3+1
K4:K13K4=VLOOKUP(I4,$B$3:$D$830,3,0)
L4:L13L4=J4*K4
N14:R23N14=PL(8)
L15L15=SUM(L4:L13)
H18:L22H18=PL(3)
H25:L30H25=PL(4)
N26:R36N26=PL(9)
H33:L39H33=PL(5)
N39:R50N39=PL(10)
H42:L49H42=PL(6)
Dynamic array formulas.
Named Ranges
NameRefers ToCells
up=DATA!$D$3:$D$830H18, H25, H33, H42, N3, N14, N26, N39, K4:K13
Lambda Functions
NameFormula
PL=LAMBDA(np,LET(f,5000000,t,"Product ",o,"0000",ta,{"No","Product","Quantity","U.P","Amount"},tp,ROWS(up),st,SEQUENCE(tp),av,AVERAGE(up),n,np-1,m,INT(f/av/np)+15,c,SEQUENCE(,m),x,INDEX(SORTBY(st,RANDARRAY(tp)),SEQUENCE(n)),y,RANDARRAY(n,,1,m,1),u,INDEX(up,x),z,u*y,s,f-SUM(z),r,FILTER(st,ISNA(XMATCH(st,x))),q,SEQUENCE(ROWS(r),m),a,INDEX(up,r)*c,b,XMATCH(a,s),i,AND(ISNA(b)),IF(i,PL(np),LET(j,MAX(ISNUMBER(b)*q),w,QUOTIENT(j-1,m)+1,l,MOD(j-1,m)+1,p,INDEX(r,w),v,INDEX(up,p),k,VSTACK(HSTACK(SEQUENCE(n),t&TEXT(x,o),y,u,z),HSTACK(np,t&TEXT(p,o),l,v,l*v)),VSTACK(ta,k,HSTACK("","TOTAL","","",SUM(TAKE(k,,-1))))))))
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,177
Members
453,021
Latest member
Justyna P

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