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
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