Find combination

Kendok

New Member
Joined
Feb 3, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi there
I got this data:
1675431084490.png


The client only sends me a total of 1220 in units and 6064.40 in price, so I have to find out which rows are part of those totals.
I have tried kutools with make a number, but it only lets you choose a search, and of course there are more than 16000 options to combine the products and that add up to 1220 but of course, if I were also able to extract only those that add up to 6064.4 in this price number would be reduced by far to 1 or two options at most.
Solver is also not an option because it doesn't allow me to add more than one criteria in sum.
The truth is that I don't know how I should do it anymore, my brain has boiled.
I don't know if I explained myself...

PS when I try to use kutools to find the combinations by price it goes crazy and quits so that's not an option either

Any ideas?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I've just realized that my previous code can not allocate (1, (2 ^ lRow) - 2) size array when there are 60 rows due to VBA limitations 😬
Here is another code will check up to 5 combinations. Check column K for results.
VBA Code:
Sub combinations()
  Dim items() As Variant
  Dim combLen As Long
  Dim lRow As Long, targetVal As Double, result As Double
  lRow = Cells(Rows.count, 1).End(xlUp).Row
  targetVal = 6064.4
 
  For c = 1 To 5
    combLen = c
    ReDim items(1 To lRow - 1)
 
    For i = 2 To lRow
      items(i - 1) = Cells(i, 7).Value
    Next
 
    items = binomial(items, combLen)

    For i = 1 To nChooseK(lRow - 1, combLen)
      result = 0
      For j = 1 To combLen
        result = result + CDbl(items(i, j))
        If result = targetVal Then
          With Application
          For k = 1 To combLen
            For n = 11 To 16
              Cells(k, n).Value = Cells(Application.Match(items(i, k), Range("G1:G" & lRow), 0), n - 10).Value
            Next
            Cells(k, 17).Value = items(i, k)
          Next
          Exit Sub
          End With
        End If
      Next
    Next
  Next
  MsgBox "No solution!"
End Sub
Function binomial(ByRef v() As Variant, r As Long) As Variant()
  Dim i As Long, k As Long, z() As Variant, comboMatrix() As Variant
  Dim numRows As Long, numIter As Long, n As Long, count As Long
   
  count = 1
  n = UBound(v)
  numRows = nChooseK(n, r)
 
  ReDim z(1 To r)
  ReDim comboMatrix(1 To numRows, 1 To r)
  For i = 1 To r
    z(i) = i
  Next
  Do While (count <= numRows)
    numIter = n - z(r) + 1
    For i = 1 To numIter
      For k = 1 To r
        comboMatrix(count, k) = v(z(k))
      Next
      count = count + 1
     z(r) = z(r) + 1
    Next
    For i = r - 1 To 1 Step -1
      If Not (z(i) = (n - r + i)) Then
        z(i) = z(i) + 1
        For k = (i + 1) To r
          z(k) = z(k - 1) + 1
        Next
        Exit For
      End If
    Next
  Loop
  binomial = comboMatrix
End Function
Function nChooseK(n As Long, k As Long) As Long
  Dim temp As Double, i As Long
  temp = 1
  For i = 1 To k
    temp = temp * (n - k + i) / i
  Next
  nChooseK = CLng(temp)
End Function
By the way, there are no possible solutions for 6064.4 for the given list.
 
Upvote 0
Something like this

1675697964058.png


In fact there are like 4 or 5 invoices. The idea would be to associate each invoice number with the list of products. Knowing that all the products have been served, but we do not know in which invoice they have been included...
 
Upvote 0
I've just realized that my previous code can not allocate (1, (2 ^ lRow) - 2) size array when there are 60 rows due to VBA limitations 😬
Here is another code will check up to 5 combinations. Check column K for results.
VBA Code:
Sub combinations()
  Dim items() As Variant
  Dim combLen As Long
  Dim lRow As Long, targetVal As Double, result As Double
  lRow = Cells(Rows.count, 1).End(xlUp).Row
  targetVal = 6064.4
 
  For c = 1 To 5
    combLen = c
    ReDim items(1 To lRow - 1)
 
    For i = 2 To lRow
      items(i - 1) = Cells(i, 7).Value
    Next
 
    items = binomial(items, combLen)

    For i = 1 To nChooseK(lRow - 1, combLen)
      result = 0
      For j = 1 To combLen
        result = result + CDbl(items(i, j))
        If result = targetVal Then
          With Application
          For k = 1 To combLen
            For n = 11 To 16
              Cells(k, n).Value = Cells(Application.Match(items(i, k), Range("G1:G" & lRow), 0), n - 10).Value
            Next
            Cells(k, 17).Value = items(i, k)
          Next
          Exit Sub
          End With
        End If
      Next
    Next
  Next
  MsgBox "No solution!"
End Sub
Function binomial(ByRef v() As Variant, r As Long) As Variant()
  Dim i As Long, k As Long, z() As Variant, comboMatrix() As Variant
  Dim numRows As Long, numIter As Long, n As Long, count As Long
  
  count = 1
  n = UBound(v)
  numRows = nChooseK(n, r)
 
  ReDim z(1 To r)
  ReDim comboMatrix(1 To numRows, 1 To r)
  For i = 1 To r
    z(i) = i
  Next
  Do While (count <= numRows)
    numIter = n - z(r) + 1
    For i = 1 To numIter
      For k = 1 To r
        comboMatrix(count, k) = v(z(k))
      Next
      count = count + 1
     z(r) = z(r) + 1
    Next
    For i = r - 1 To 1 Step -1
      If Not (z(i) = (n - r + i)) Then
        z(i) = z(i) + 1
        For k = (i + 1) To r
          z(k) = z(k - 1) + 1
        Next
        Exit For
      End If
    Next
  Loop
  binomial = comboMatrix
End Function
Function nChooseK(n As Long, k As Long) As Long
  Dim temp As Double, i As Long
  temp = 1
  For i = 1 To k
    temp = temp * (n - k + i) / i
  Next
  nChooseK = CLng(temp)
End Function
By the way, there are no possible solutions for 6064.4 for the given list.
1675698089593.png


it returns an error :(
 
Upvote 0
It was my first code. try the this code:
I've just realized that my previous code can not allocate (1, (2 ^ lRow) - 2) size array when there are 60 rows due to VBA limitations 😬
Here is another code will check up to 5 combinations. Check column K for results.
VBA Code:
Sub combinations()
  Dim items() As Variant
  Dim combLen As Long
  Dim lRow As Long, targetVal As Double, result As Double
  lRow = Cells(Rows.count, 1).End(xlUp).Row
  targetVal = 6064.4
 
  For c = 1 To 5
    combLen = c
    ReDim items(1 To lRow - 1)
 
    For i = 2 To lRow
      items(i - 1) = Cells(i, 7).Value
    Next
 
    items = binomial(items, combLen)

    For i = 1 To nChooseK(lRow - 1, combLen)
      result = 0
      For j = 1 To combLen
        result = result + CDbl(items(i, j))
        If result = targetVal Then
          With Application
          For k = 1 To combLen
            For n = 11 To 16
              Cells(k, n).Value = Cells(Application.Match(items(i, k), Range("G1:G" & lRow), 0), n - 10).Value
            Next
            Cells(k, 17).Value = items(i, k)
          Next
          Exit Sub
          End With
        End If
      Next
    Next
  Next
  MsgBox "No solution!"
End Sub
Function binomial(ByRef v() As Variant, r As Long) As Variant()
  Dim i As Long, k As Long, z() As Variant, comboMatrix() As Variant
  Dim numRows As Long, numIter As Long, n As Long, count As Long
  
  count = 1
  n = UBound(v)
  numRows = nChooseK(n, r)
 
  ReDim z(1 To r)
  ReDim comboMatrix(1 To numRows, 1 To r)
  For i = 1 To r
    z(i) = i
  Next
  Do While (count <= numRows)
    numIter = n - z(r) + 1
    For i = 1 To numIter
      For k = 1 To r
        comboMatrix(count, k) = v(z(k))
      Next
      count = count + 1
     z(r) = z(r) + 1
    Next
    For i = r - 1 To 1 Step -1
      If Not (z(i) = (n - r + i)) Then
        z(i) = z(i) + 1
        For k = (i + 1) To r
          z(k) = z(k - 1) + 1
        Next
        Exit For
      End If
    Next
  Loop
  binomial = comboMatrix
End Function
Function nChooseK(n As Long, k As Long) As Long
  Dim temp As Double, i As Long
  temp = 1
  For i = 1 To k
    temp = temp * (n - k + i) / i
  Next
  nChooseK = CLng(temp)
End Function
By the way, there are no possible solutions for 6064.4 for the given list.
 
Upvote 0
It was my first code. try the this code:
ouch...
1675698624416.png

the question is that there MUST be a solution, because the list is the breakdown of the shipment for its importation, so it cannot be that there is no solution... :-s
Thanks for the help! :)
 
Upvote 0
I've already wrote in the post. This code checks from 1 to 5 combinations of every record. There is no possible solution for 6064.4 in this dataset. At least up to 5 records...

Ok try to set target value to 1026.8 and rerun. You'll understand what I mean.

VBA Code:
targetVal = 1026.8
 
Upvote 0
I've already wrote in the post. This code checks from 1 to 5 combinations of every record. There is no possible solution for 6064.4 in this dataset. At least up to 5 records...

Ok try to set target value to 1026.8 and rerun. You'll understand what I mean.

VBA Code:
targetVal = 1026.8
oki oki, thnx!
 
Upvote 0
Solver Solution:
16,21,41,43,44,45,46

Combinations of Column Values that Add up to a Value V1.xlsm
ABCDEFGHIJKLMNOPQ
1DescripT1CountryFindT2Unity Price Target 1Target 2DifferencePrice DifferenceSolution
21NL1253.0411253.0412206064.433097379.7816,21,41,43,44,45,46
32NL1256.8811256.88
43NL1225061122506
54NL1002851100285
65NL90502.38190502.38
76NL60360160360
87NL5054.515054.5
98NL2461.812461.8
109NL30121.05130121.05
1110NL3056.413056.4
1211NL3669.8413669.84
1312NL50110150110
1413NL404614046
1514NL65416.5165416.5
1615NL48408148408
1716NL1844.111844.1
1817NL4090.814090.8
1918NL70402.5170402.5
2019NL117412.81117412.8
2120NL32440132440
2221NL1001351100135
2322NL144211.681144211.68
2423NL5063.515063.5
2524NL96168.96196168.96
2625NL4260.4814260.48
2726NL30103.2130103.2
2827NL1872.7211872.72
2928NL1872.7211872.72
3029NL1248.4811248.48
3130NL1801081180108
3231NL2450.8812450.88
3332NL10551.45110551.45
3433NL9657.619657.6
3534NL4005001400500
3635NL3073.213073.2
3736NL6511651
3837NL6451645
3938NL506915069
4039NL407814078
4140NL48477.6148477.6
4241NL54137.7154137.7
4342NL804370818043708
4443NL72284.4172284.4
4544NL120386.41120386.4
4645NL96360196360
4746NL72109.44172109.44
4847NL1860.1211860.12
4948NL20100.4120100.4
5049NL2444.8812444.88
5150NL363613636
5251NL7262.6417262.64
5352NL112131.681112131.68
5453NL96249.6196249.6
5554NL130189.81130189.8
5655NL30135130135
5756NL908119081
5857NL72178.56172178.56
5958NL3596.2513596.25
6059NL3596.2513596.25
61
62452913444.18
63
Sheet2
Cell Formulas
RangeFormula
I2:I60I2=F2*H2
J2:J60J2=G2*H2
M2:N2M2=I62-K2
I62:J62I62=SUM(I2:I60)
Named Ranges
NameRefers ToCells
solver_adj=Sheet2!$H$2:$H$60I2:J2
solver_lhs1=Sheet2!$H$2:$H$60I2:J2
solver_lhs2=Sheet2!$H$2:$H$60I2:J2


Solver Settings.jpg
 
Upvote 0
Solution

Forum statistics

Threads
1,224,607
Messages
6,179,871
Members
452,948
Latest member
UsmanAli786

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