I have a problem I have solved using several nested while loops. But unfortunately this means it takes many hours to run as doing it like this makes millions of iterations.
I would like to know if anyone can suggest a better method. I’ll describe the problem in a standard products and profit manner. I have 5 different product pages, each containing 100 products with their cost and the profit that will be made on them. I have to buy 2 products from one page and 3 from the others. I need to find the best combination to maximize profit based on having 10000 to spend (I can also only buy one of each product).
The code I have looks like the following, but as this takes so long and often crashes excel it is of no real use.
Do While productOneCount <= totalNumberOfProductOne
productOneCost = Worksheets("Product One").Range("C" & productOneCount)
productOneProfit = Worksheets("Product One").Range("E" & productOneCount)
secondProductOneCount = productOneCount + 1
Do While secondProductOneCount <= totalNumberOfProductOne
secondProductOneCost = Worksheets("Product One").Range("C" & secondProductOneCount)
secondProductOneProfit = Worksheets("Product One").Range("E" & secondProductOneCount)
thirdProductOneCount = secondProductOneCount + 1
Do While thirdProductOneCount <= totalNumberOfProductOne
thirdProductOneCost = Range("C" & Worksheets("Product One").thirdProductOneCount)
thirdProductOneProfit = Range("E" & Worksheets("Product One").thirdProductOneCount)
productTwoCount = 1
Do While productTwoCount <= totalNumberOfProductTwo
productTwoCost = Worksheets("Product Two").Range("C" & productTwoCount)
productTwoProfit = Worksheets("Product Two").Range("E" & productTwoCount)
secondProductTwoCount = productTwoCount + 1
Do While secondProductTwoCount <= totalNumberOfProductTwo
secondProductTwoCost = Range("C" & secondProductTwoCount)
secondProductTwoProfit = Range("E" & secondProductTwoCount)
thirdProductTwoCount = secondProductTwoCount + 1
' this goes on for all 5 different products
totalCost = productOneCost + secondProductOneCost + thirdProductOneCost + productTwoCost + secondProductTwoCost + restOfProductCosts
totalProfit = productOneProfit + secondProductOneProfit + thirdProductOneProfit + productTwoProfit + secondProductTwoProfit + restOfProductProfit
If totalCost <= 10000 Then
If totalProfit > bestProfit Then
Worksheets("Buy").Range("A1") = Worksheets("Product One").Range("B" & productOneCount)
Worksheets("Buy").Range("A2") = Worksheets("Product One").Range("B" & secondProductOneCount)
Worksheets("Buy").Range("A3") = Worksheets("Product One").Range("B" & thirdProductOneCount)
Worksheets("Buy").Range("A4") = Worksheets("Product Two").Range("B" & productTwoCount)
Worksheets("Buy").Range("A5") = Worksheets("Product Two").Range("B" & secondProductTwoCount)
Worksheets("Buy").Range("B1") = totalCost
Worksheets("Buy").Range("B2") = totalProfit
bestProfit = totalProfit
End If
End If
secondProductTwoCount = secondProductTwoCount + 1
Loop
productTwoCount = productTwoCount + 1
Loop
thirdProductOneCount = thirdProductOneCount + 1
Loop
secondProductOneCount = secondProductOneCount + 1
Loop
productOneCount = productOneCount + 1
Loop