Creating macro for nested calculations within calculations

LizN

New Member
Joined
Sep 30, 2024
Messages
3
Office Version
  1. 365
Platform
  1. Windows
Hi everyone,

I was hoping to get advice on creating a macro for calculating values which are then used in subsequent calculations (which are then often used in subsequent calculations).
The project I'm working on is using a food composition database, which has a list of 5700 odd foods. I am trying to assign values to each food based on their composition (called X values for this post). Some of these foods are "recipes", whereby there is a list of the ingredients in each food (for example the food item beef cooked in oil has the ingredients 'beef' and 'oil' and their proportions in the overall food). To calculate the X values for recipe foods, I need to calculate the X value of all of the individual ingredients, multiply them by the proportion they are present in the original food, then add them together. I've been doing this manually with the help of some VLOOKUPs and pivot tables, and overall it has been working ok, though very time-consuming.

The problem arises because some of these ingredients within each food have recipes themselves, so in the 'beef, cooked in oil' example, the oil itself is recipe of multiple different types of oil. So basically I need to calculate the X value for the oil, then paste it to the spreadsheet, then calculate the X value of the 'beef, cooked in oil'. Some recipes have multiple levels of recipes within them, up to 5 or 6 levels of recipe. I was doing all these manually and getting through them, but have realised I made an error with calculating the X value for a food which is an ingredient in many recipes (which themselves are ingredients within recipes etc), so I'm hoping there might be some way to speed up this process.

I've attached screenshots of an example workbook to show what I'm meaning (sorry, my work laptop which I'm currently on doesn't allow me to download xl2bb, but should be able to do so later tonight if anyone would like have the workbook). In the sheet 'main food list', column A is the survey ID, which is the unique ID for each food, which I use to link sheets etc, and column C is the current X value (though this needs to change for some foods if the ingredient X value changes). This sheet normally has over 5700 foods, all of which will have X values. The second sheet 'recipe list' is the list of ingredients for foods in the 'main food list' which have multiple ingredients (the ingredients themselves are normally all items within the 'main food list'). In this sheet, column A is the ID of the main food, column D is the ID of the ingredient within the main food, column C is the X value of the ingredient, column G is the proportion of the ingredient within the main food (e.g. the food 18101022 'Beef, blade steak, untrimmed, baked, roasted, fried, grilled or BBQ'd, fat not further defined' is 98% beef and 2% oil), and column H is the X value multiplied by the proportion. Using pivot tables (not included in this example) I then sum the X values for each food in column H to get the overall X value for the food. When recipes are ingredients within another recipe, I have been manually pasting the calculated X value into the 'recipe list', for example the orange value for ingredient ID 14602003 is the sum of the X values for the rows 2 - 19.
It gets really tricky if I need to change any of these X values. In the example spreadsheet ('recipe list'), if I need to update the X value listed in column C for 14401010 (row 13), then I need to manually recalculate and replace all of the X values for the foods they are ingredients in, and then the foods they are ingredients in etc.

Sorry I know this is hugely confusing, but I was wondering if anyone has any tips on writing a macro to cover this, or even if it is do-able at all? I'm fairly proficient with Excel formulas and pivot tables, but am a complete novice with macros.

Thank you very much,
Liz
 

Attachments

  • Screenshot 2024-10-01 102549.png
    Screenshot 2024-10-01 102549.png
    54.5 KB · Views: 7
  • Screenshot 2024-10-01 102700.png
    Screenshot 2024-10-01 102700.png
    190.7 KB · Views: 7

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Main food list sheet:
MrExcel sheet example.xlsx
ABC
1Survey IDFood NameFinal X value per gram
218101022Beef, blade steak, untrimmed, baked, roasted, fried, grilled or BBQ'd, fat not further defined0.01640567
Main food list
 
Upvote 0
'Recipe list' sheet

MrExcel sheet example.xlsx
ABCDEFGH
1Food Survey IDFood NameIngredient X value per gramIngredient Survey IDDoes the ingredient have a recipe?Ingredient NameWeight as % of recipe totalX value (calculated as proportion of product)
214602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0032614402007NOOil, olive0.5879591270.001917
314602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0011614402003NOOil, canola0.1264843970.000147
414602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0013514401009NOOil, sunflower0.033692354.55E-05
514602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0018914402008NOOil, peanut0.0204363443.86E-05
614602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.00210314402009NOOil, rice bran0.0204363444.3E-05
714602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.00144714401003NOOil, grapeseed0.0035901685.19E-06
814602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.00210314402005NOOil, macadamia0.0013808342.9E-06
914602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.00144714401005NOOil, maize0.0022093343.2E-06
1014602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0023114401007NOOil, sesame0.0082850041.91E-05
1114602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.00144714401004NOOil, linseed or flaxseed0.0002761674E-07
1214602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0006814401008NOOil, soybean0.0013808349.39E-07
1314602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.00177514401010YESOil, vegetable0.1068765530.00019
1414602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0058714502002NOOil, copha0.0005523343.24E-06
1514602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.00439414503001NOFat, solid, blend of animal & vegetable oils0.0005523342.43E-06
1614602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0083714101004YESButter, not further defined0.0566141950.000474
1714602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.00596114201001NODairy blend, butter & edible oil spread (~80% fat), sodium 485 mg/100 g0.0024855011.48E-05
1814602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0083714102001NOGhee, clarified butter0.006904175.78E-05
1914602003Fat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.0011314307005YESMargarine spread, monounsaturated or polyunsaturated, regular fat (>50% fat), not further defined0.019884012.25E-05
2018101022Beef, blade steak, untrimmed, baked, roasted, fried, grilled or BBQ'd, fat not further defined0.0166818101020YESBeef, blade steak, untrimmed, raw0.980.016346
2118101022Beef, blade steak, untrimmed, baked, roasted, fried, grilled or BBQ'd, fat not further defined0.00298714602003YESFat or oil, not further defined, for use in home cooked meat, fish, poultry & mixed dish recipes0.025.97E-05
Recipe list
 
Upvote 0

Forum statistics

Threads
1,223,785
Messages
6,174,539
Members
452,571
Latest member
MarExcelTips

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