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

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
is KUTOOLS a differerent language version of a function? If so, do you know the english name of the function?
is the price of the unit 6064.40 / 1220, what row number in the image you sent fits that scenario?

Can you use the xl2bb add in so you can paste a mini workbook with examples of the scenario? See link below.
 
Upvote 0
Libro1etgfdfgdh.xlsx
ABCDEFG
1DescripT1CountryFindT2Unity Price
2001NL1253,04 €
3002NL1256,88 €
4003NL122506,00 €
5004NL100285,00 €
6005NL90502,38 €
7006NL60360,00 €
8007NL5054,50 €
9008NL2461,80 €
10009NL30121,05 €
11010NL3056,40 €
12011NL3669,84 €
13012NL50110,00 €
14013NL4046,00 €
15014NL65416,50 €
16015NL48408,00 €
17016NL1844,10 €
18017NL4090,80 €
19018NL70402,50 €
20019NL117412,80 €
21020NL32440,00 €
22021NL100135,00 €
23022NL144211,68 €
24023NL5063,50 €
25024NL96168,96 €
26025NL4260,48 €
27026NL30103,20 €
28027NL1872,72 €
29028NL1872,72 €
30029NL1248,48 €
31030NL180108,00 €
32031NL2450,88 €
33032NL10551,45 €
34033NL9657,60 €
35034NL400500,00 €
36035NL3073,20 €
37036NL651,00 €
38037NL645,00 €
39038NL5069,00 €
40039NL4078,00 €
41040NL48477,60 €
42041NL54137,70 €
43042NL8043.708,00 €
44043NL72284,40 €
45044NL120386,40 €
46045NL96360,00 €
47046NL72109,44 €
48047NL1860,12 €
49048NL20100,40 €
50049NL2444,88 €
51050NL3636,00 €
52051NL7262,64 €
53052NL112131,68 €
54053NL96249,60 €
55054NL130189,80 €
56055NL30135,00 €
57056NL9081,00 €
58057NL72178,56 €
59058NL3596,25 €
60059NL3596,25 €
Hoja1 (2)


Libro1etgfdfgdh.xlsx
ABC
1Num InvUnitTotal
260290489302.385,52 €
Hoja5



So in the first mini sheet you can see a list of products with their prices and number of units.
The customer only notifies the total number of units purchased and the total price of all of them.
NOTE: If the customer has selected any product, they have purchased all available units. And the total of these units is the content in the column "price"
Thus, if the customer has selected the item id: 44, he has purchased the 120 units for a total of 386.40.
The issue is to find all those products that may have been included in the invoice, knowing that when adding both the units and the price, it must coincide with the total of the invoice.
 
Upvote 0
Sorry, this one
Libro1etgfdfgdh.xlsx
ABC
1Num InvUnitTotal
2602905012206.064,40 €
Hoja5
 
Upvote 0
So I was working on this problem for some time, and I was able to create a combination of 6064.40 pretty easily, the problem occurs when I try to add one more condition to it then my excel crashes. I'm using a dictionary combination, I will refine the code and try to find a solution. Dictionary with a class module is what I'm thinking right now. If I get a solution I'll get back to you. Are we allowed to have duplicate values of unit or should they all be unique?
 
Upvote 0
I think this should work for you :)
VBA Code:
Sub permutation()
  Dim lRow As Integer, targetVal As Double
  lRow = Cells(Rows.Count, 1).End(xlUp).Row
  ReDim inputt(1, lRow - 1) As String
  ReDim outputt(1, (2 ^ lRow) - 2) As String

  targetVal = 6064.40
  For i = 2 To lRow
    inputt(0, i - 2) = Cells(i, 1).Value
    inputt(1, i - 2) = Cells(i, 7).Value
  Next
  For i = 0 To lRow - 1
    outputt(0, (2 ^ i) - 1) = inputt(0, i)
    outputt(1, (2 ^ i) - 1) = inputt(1, i)
    If CDbl(outputt(1, (2 ^ i) - 1)) = targetVal Then
      Cells(1, 10).Value = outputt(0, (2 ^ i) - 1)
      Exit For
    End If
    For ii = 0 To (2 ^ i) - 2
      outputt(1, (2 ^ i) + ii) = CDbl(outputt(1, ii)) + CDbl(inputt(1, i))
      outputt(0, (2 ^ i) + ii) = outputt(0, ii) & "," & inputt(0, i)
      If CDbl(outputt(1, (2 ^ i) + ii)) = targetVal Then
        temp = Split(outputt(0, (2 ^ i) + ii), ",")
        For j = 1 To UBound(temp)
          r = Application.Match(temp(j), Range("A:A"), 0)
          Cells(j, 9).Value = temp(j)
          Range("J" & j & ":O" & j).Value = Range("B" & r & ":G" & r).Value
        Next
        Exit Sub
      End If
    Next
  Next
  MsgBox "No possible solution!"
End Sub
This will check all combinations as long as you have enough time 😬
 
Upvote 0
I have an idea to approach this very difficult problem, but most likely I won't be able to start looking at it until tomorrow.
 
Upvote 0
Forgot to mention:

@Kendok Can you provide a sample result that you would be looking for? You supplied some sample data, but you haven't supplied what the result would look like for the sample data provided.
 
Upvote 0

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