VBA Recipe calculator sum the ingredients

Exceladd1ct

Board Regular
Joined
Feb 10, 2019
Messages
76
Hello,

I need a piece of code that will calculate the quantities of needed ingredients for some products but i am stuck with some 2d arrays and the code messed up. Can you please help me?
In the attached images,
-the result for ingredient1 is 40 because we need 20 cakes and we use 2 pcs in each cake.
-the result for ingredient15 is 30 because we use 1 pcs in each cake but it is needed in both cake1 and cake3
-and so on

I will run the code with a button or a shape on the worksheet.
We can asume the columns for RECIPES sheet are A,B,C,D and for CALCULATOR sheet are A,B and E,F,G respectively.

Thank you!
 

Attachments

  • 2021-02-10_17h34_26.png
    2021-02-10_17h34_26.png
    21.5 KB · Views: 40
  • 2021-02-10_17h34_43.png
    2021-02-10_17h34_43.png
    20.1 KB · Views: 38

Excel Facts

VLOOKUP to Left?
Use =VLOOKUP(A2,CHOOSE({1,2},$Z$1:$Z$99,$Y$1:$Y$99),2,False) to lookup Y values to left of Z values.
Hi,

Use below code:

VBA Code:
Sub showCalculation()
Dim totalRows As Integer, rowno As Integer
Dim totalProds As Integer, prodNo As Integer
Dim resultRow As Integer

totalRows = Sheets("Recipes").Cells(Rows.Count, 1).End(xlUp).Row
totalProds = Sheets("Calculator").Cells(Rows.Count, 1).End(xlUp).Row

resultRow = 3

Sheets("Calculator").Range("E3:G" & Cells(Rows.Count, 5).End(xlUp).Row).ClearContents

    For prodNo = 3 To totalProds
        For rowno = 3 To totalRows
            With Sheets("Calculator")
                If .Cells(prodNo, 1) = Sheets("Recipes").Cells(rowno, 1) Then
                    .Cells(resultRow, "E") = Sheets("Recipes").Cells(rowno, 2)
                    .Cells(resultRow, "F") = Sheets("Recipes").Cells(rowno, 3)
                    .Cells(resultRow, "G") = Sheets("Recipes").Cells(rowno, 4) * .Cells(prodNo, "B")
                    resultRow = resultRow + 1
                End If
            End With
        Next
    Next
End Sub


Cake Recipe.xlsm
ABCDEFGH
1
2ProductQuantityIngredientIngredient IDQty Needed
3Cake225Ingredient2ID225
4Ingredient8ID850
5Ingredient9ID925
6Ingredient16ID1650
7
8
9
10
11
12
13
14
15
16
17
Calculator


Cake Recipe.xlsm
ABCD
2ProductIngredientsIngredient IDNeeded Quantity
3Cake1Ingredient1ID12
4Cake1Ingredient2ID21
5Cake1Ingredient3ID32
6Cake1Ingredient15ID152
7Cake1Ingredient5ID53
8Cake1Ingredient6ID64
9Cake2Ingredient2ID21
10Cake2Ingredient8ID82
11Cake2Ingredient9ID91
12Cake2Ingredient16ID162
13Cake3Ingredient21ID212
14Cake3Ingredient11ID114
15Cake3Ingredient22ID223
16Cake3Ingredient33ID335
17Cake3Ingredient34ID343
Recipes
 
Upvote 0
Solution
Your code works great, but when you add a second product (eg. cake3), instead of summing up the common ingredients, it adds them to the results.
So i had to add a dictionary to your code and now it does the job.

Thank you for your contribution.

VBA Code:
Sub showCalculation()
Dim totalRows As Integer, rowno As Integer
Dim totalProds As Integer, prodNo As Integer
Dim resultRow As Integer, new_value As Long

Dim dict As Object

Set dict = CreateObject("Scripting.Dictionary")

totalRows = Sheets("Recipes").Cells(Rows.Count, 1).End(xlUp).Row
totalProds = Sheets("Calculator").Cells(Rows.Count, 1).End(xlUp).Row

resultRow = 3

Sheets("Calculator").Range("E3:G" & Cells(Rows.Count, 5).End(xlUp).Row).ClearContents

    For prodNo = 3 To totalProds
        For rowno = 3 To totalRows
            With Sheets("Calculator")
                If .Cells(prodNo, 1) = Sheets("Recipes").Cells(rowno, 1) Then
                
                    ingredient_name = Sheets("Recipes").Cells(rowno, 2)
                    ingredient_id = Sheets("Recipes").Cells(rowno, 3)
                    ingredient_qty = Sheets("Recipes").Cells(rowno, 4) * .Cells(prodNo, "B")
                    
                    If Not dict.exists(ingredient_name) Then
                        dict.Add ingredient_name, ingredient_qty
                    Else
                        new_value = ingredient_qty
                        dict.Item(ingredient_name) = dict.Item(ingredient_name) + new_value
                    End If
                    
                    resultRow = resultRow + 1
                    
                End If
            End With
        Next
    Next
    
    
Dim key As Variant
For Each key In dict.Keys
    lrow = Sheets("Calculator").Cells(Rows.Count, 5).End(xlUp).Row
    Sheets("Calculator").Cells(lrow + 1, 5).Value = key
    Sheets("Calculator").Cells(lrow + 1, 6).Value = dict(key)
Next key
    
    
End Sub
 
Upvote 0
It's great that the problem resolved. Thanks for the feedback.
 
Upvote 0

Forum statistics

Threads
1,218,277
Messages
6,141,497
Members
450,365
Latest member
robsandersppc

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