How to list ingredients in a recipe when I have "single ingredients" as well as "complex ingredients"

gagasp

New Member
Joined
Jun 2, 2022
Messages
5
Office Version
  1. 2019
Platform
  1. Windows
Background. A recipe is made of single ingredients as well as "complex ingredients". As an example if I make a lasagna I will use straight ingredients (i.e. parmesan, dry pasta) as well as "complex ingredients" (i.e. ragu sauce). "Complex ingredients" are made of single ingredients (i.e. tomato sauce, ground pork, ground beef). The recipe will call for certain amount of parmesan, dry pasta and ragu but I need to substitute in the ingredients required for purchase the individual ingredients that the "complex ingredient" is made of. Basically in the pivot table i need to be able to substitute the complex ingredient with the breakdown of the ingredients that are contained in the "complex ingredient". I haven't found any example of such a thing in my search. I didn't see any way around using pivot table which appears to be used for merging columns rather than doing more complex operation and I am left with trying building a macro. I am posting this message to see if anybody else came across a similar problem. Thanks in advance for any help on this matter
gg
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
gagasp.xlsb
with a macro, 3 tables and a pivottable
VBA Code:
Option Compare Text

Sub Shoppinglist()
     Dim Dict, aComplex, aList1
     Set Dict = CreateObject("scripting.dictionary")     'your shoppinglist
     Dict.comparemode = vbTextCompare     'is case insensitive

     With Sheets("blad1")     'this worksheet
          aComplex = .ListObjects("TBL_Complex").DataBodyRange.Value     'the complex ingredients
          aList1 = .ListObjects("TBL_List1").DataBodyRange.Value     'the initial shoppinglist with simple and complex ingredients
          For i = 1 To UBound(aList1)     'loop throug that list
               If aList1(i, 3) <> "x" Then     '3rd column not "x" = simple ingredient
                    Dict.Add Dict.Count, Array(aList1(i, 1), aList1(i, 2), aList1(i, 3), aList1(i, 4))     'copy then index, naam, unit and amount to the shoppinglist
               Else     'it's a complex ingredient
                    For j = 1 To UBound(aComplex)     'loop through the complex list
                         If aComplex(j, 1) = aList1(i, 2) Then     'same name
                              Dict.Add Dict.Count, Array(aList1(i, 1), aComplex(j, 2), aComplex(j, 3), aComplex(j, 4) * aList1(i, 4))     'add index, complex subname, complex subunit, complex amount multiplied by the initial amount
                         End If
                    Next
               End If
          Next

          i = Dict.Count     'number of lines
          With .ListObjects("TBL_List2")     'the extended list
               If .ListRows.Count Then .DataBodyRange.Delete     'start empty if it wasn't
               If i > 0 Then     'there are items
                    If i = 1 Then Dict.Add Dict.Count, Array("", "", "", "")     'bug when only 1 item, add an empty row with equal elements
                    arr = Application.Index(Dict.items, 0, 0)     'read items to an array
                    .ListRows.Add.Range.Range("A1").Resize(i, UBound(arr, 2)).Value = arr     ' write array to table
               End If
               .HeaderRowRange.EntireColumn.AutoFit
          End With

          ThisWorkbook.RefreshAll
          .PivotTables(1).TableRange1.EntireColumn.AutoFit

     End With


End Sub
gagasp.xlsb
ABCDEFGHIJKLMNOPQRS
1complex nameingredientunitamount indexshoppinglistunitamountindexshoppinglistunitamountShoppinglist
2ragu sauce / 10tomato saucecan 500 ml21parmesankg51parmesankg5ingredientsunitTotaal
3ragu sauce / 10ground porkkg1,52dry pasta500 g102dry pasta500 g10dry pasta1000 g10
4ragu sauce / 10ground beefkg23ragu sauce / 10x0,53tomato saucecan 500 ml1dry pasta500 g10
5ragu sauce / 10saltspoon14ground beefkg153ground porkkg0,75ground beefkg20,6
6ragu sauce / 10waterliter15dry pasta1000 g103ground beefkg1ground porkkg4,2
76ragu sauce / 10x2,33saltspoon0,5parmesankg5
83waterliter0,5saltspoon2,8
94ground beefkg15tomato saucecan 500 ml5,6
105dry pasta1000 g10waterliter2,8
116tomato saucecan 500 ml4,6
126ground porkkg3,45
136ground beefkg4,6
146saltspoon2,3
156waterliter2,3
16
17
18
Blad1
Cell Formulas
RangeFormula
F2:F7F2=ROW()-1
 
Upvote 0
Solution
Thanks for the prompt reply and for the codes. I will try in my environment and report back. I am definitely happy that you understood my problem giving my rough description of it. On another note is it possible to download your spreadsheet and test it with your macro before I import it in my files?
Thanks again
gagasp
 
Upvote 0
So far it looks very good. I didn't know how to manage the xlsb and I didnt have the xl2bb add-in but now I am set. While playing around with it the pivot disappeared and I don't know what happened. I have reloaded it and I will test more to see what I did wrong and if I am able to replicate it. So far you are definitely my hero. As soon as I will migrate to my environment which is a fairly big one I will be able to declare victor and make you my HERO all capital.
Thanks again
gio
 
Upvote 0
I was able to move the table and and make modifications that could be used in my real environment however I need your last push as I need to have the "complex ingredients" in a different tab and also the pivot table. I am trying to do it myself but If you can give me a little push I would appreciate
thanks again
gio
 
Upvote 0

Forum statistics

Threads
1,223,908
Messages
6,175,306
Members
452,633
Latest member
DougMo

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