Nested VLOOKUP Alternative

Keeper4826

New Member
Joined
Nov 6, 2006
Messages
47
On my main sheet, I have a row with two drop down lists. In column B, you select a food category. In column C, you select a specific food item based on the food category selected. The remaining columns to the right of that are for the macro nutrients of the selected food item (carbs, sugar, protein, etc.). There is a one sheet for each food category, each with a table listing out the food items in that category and their individual macro data. There are a total of 7 sheets for the various categories. An additional sheet has been created with a table which correlates food category to corresponding table. For example, the "Fruit" category is on the Fruits sheet, which has Table3. My goal to populate the macro nutrient data of a select food item on the main sheet, pulling that data from the appropriate table based on the food category select.

The first step was to create a VLOOKUP formula based on the food item. I got that to work just fine.
=VLOOKUP(C3,Table4,3,FALSE) 'C3 is the individual item to be search for, Table4 is the table that matches the category of the item selected.

My challenge now is - how do I determine which table is appropriate for the search? My example is hardcoded. What I really need is a nested VLOOKUP based on the data in B3, which would return the appropriate Table value based on my category/table reference sheet.
=VLOOKUP(C3,(VLOOKUP(B3,Table8,2,FALSE)),3,FALSE) 'Searching able8 would return the proper table for the category selected in B3.

This appears to be an invalid formula, as nesting VLOOKUPs is not allowed. How do I solve this issue?

Once I crack this nut, I'd like to know if it's possible to return multiple values based on the search results. This would allow me to input one function in a single cell, but populate five cells (for the various macro data).
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
You might be able to do this with index and Match and possibly using the Indirect function. Another guaranteed way of doing this is to use VBa and variant arrays which is much faster and more flexible that using vlookup. I am not sure of the layout of you data so I can't write any code to help you. You could look at this thread which shows how to use VBA as an alternative to Vlookup. VBA has the advantage you can nest the "lookups" to any depth you like. Also you can return multiple values very easily
https://www.mrexcel.com/forum/excel-questions/1043185-vlookup-vba-alternative.html
 
Last edited:
Upvote 0
Thank you. Here are some of the sheets I'm working with.

This is the main sheet:

Unknown 64 bit

[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][th]
G
[/th][th]
H
[/th][th]
I
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]
Meal​
[/td][td]
Category​
[/td][td]
Item​
[/td][td]
Qty​
[/td][td]
Carbs​
[/td][td]
Fat​
[/td][td]
Protein​
[/td][td]
Sugar​
[/td][td]
Calories​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]
MONDAY​
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]
Breakfast​
[/td][td]
Meats_Eggs_Nuts​
[/td][td]Egg (large)[/td][td][/td][td]
0.38g​
[/td][td]
4.97g​
[/td][td]
6.29g​
[/td][td]
0.38g​
[/td][td]
74.00g​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
11
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
12
[/td][td]Total[/td][td][/td][td][/td][td][/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Example[/td][/tr][/table]


This is an example of one of the category sheets (this one is Table4):

Unknown 64 bit

[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][th]
G
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td=bgcolor:#ED7D31]Meal Item[/td][td=bgcolor:#ED7D31]Qty[/td][td=bgcolor:#ED7D31]Carbs[/td][td=bgcolor:#ED7D31]Fat[/td][td=bgcolor:#ED7D31]Protein[/td][td=bgcolor:#ED7D31]Sugar[/td][td=bgcolor:#ED7D31]Calories[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td=bgcolor:#FCE4D6]Chicken Thigh (skinless)[/td][td=bgcolor:#FCE4D6]
4oz​
[/td][td=bgcolor:#FCE4D6]
0.00g​
[/td][td=bgcolor:#FCE4D6]
10.79g​
[/td][td=bgcolor:#FCE4D6]
25.72g​
[/td][td=bgcolor:#FCE4D6]
0.00g​
[/td][td=bgcolor:#FCE4D6]
207kcal​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]Salmon[/td][td]
4oz​
[/td][td]
0.00g​
[/td][td]
6.72g​
[/td][td]
24.52g​
[/td][td]
0.00g​
[/td][td]
166kcal​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td=bgcolor:#FCE4D6]Lamb[/td][td=bgcolor:#FCE4D6]
3oz​
[/td][td=bgcolor:#FCE4D6]
0.00g​
[/td][td=bgcolor:#FCE4D6]
17.66g​
[/td][td=bgcolor:#FCE4D6]
20.68g​
[/td][td=bgcolor:#FCE4D6]
0.00g​
[/td][td=bgcolor:#FCE4D6]
248kcal​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]Ground Turkey[/td][td]
100g​
[/td][td]
0.00g​
[/td][td]
13.04g​
[/td][td]
27.13g​
[/td][td]
0.00g​
[/td][td]
233kcal​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td=bgcolor:#FCE4D6]Egg (large)[/td][td=bgcolor:#FCE4D6]
1x​
[/td][td=bgcolor:#FCE4D6]
0.38g​
[/td][td=bgcolor:#FCE4D6]
4.97g​
[/td][td=bgcolor:#FCE4D6]
6.29g​
[/td][td=bgcolor:#FCE4D6]
0.38g​
[/td][td=bgcolor:#FCE4D6]
74kcal​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]Cashews[/td][td]
1oz​
[/td][td]
8.56g​
[/td][td]
12.43g​
[/td][td]
5.17g​
[/td][td]
1.68g​
[/td][td]
157kcal​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td=bgcolor:#FCE4D6]Pistacios[/td][td=bgcolor:#FCE4D6]
1oz​
[/td][td=bgcolor:#FCE4D6]
7.93g​
[/td][td=bgcolor:#FCE4D6]
12.60g​
[/td][td=bgcolor:#FCE4D6]
5.84g​
[/td][td=bgcolor:#FCE4D6]
2.17g​
[/td][td=bgcolor:#FCE4D6]
158kcal​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]Walnuts[/td][td]
1oz​
[/td][td]
3.89g​
[/td][td]
18.49g​
[/td][td]
4.32g​
[/td][td]
0.74g​
[/td][td]
185kcal​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td=bgcolor:#FCE4D6]Tilapia[/td][td=bgcolor:#FCE4D6]
1oz​
[/td][td=bgcolor:#FCE4D6]
0.00g​
[/td][td=bgcolor:#FCE4D6]
0.48g​
[/td][td=bgcolor:#FCE4D6]
5.69g​
[/td][td=bgcolor:#FCE4D6]
0.00g​
[/td][td=bgcolor:#FCE4D6]
27kcal​
[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Meats, Eggs, Nuts[/td][/tr][/table]


This is the sheet that has the reference table that correlates food category to appropriate table:

Unknown 64 bit

[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td=bgcolor:#000000]
Category
[/td][td=bgcolor:#000000]
Table
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td=bgcolor:#D9D9D9]
[/td][td=bgcolor:#D9D9D9]
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]Bread_Cereal_Rice_Pasta[/td][td]Table1[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td=bgcolor:#D9D9D9]Vegetables[/td][td=bgcolor:#D9D9D9]Table2[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]Fruit[/td][td]Table3[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td=bgcolor:#D9D9D9]Meats_Eggs_Nuts[/td][td=bgcolor:#D9D9D9]Table4[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]Milk_Yogurt_Cheese[/td][td]Table5[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td=bgcolor:#D9D9D9]Misc[/td][td=bgcolor:#D9D9D9]Table7[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]Beverage[/td][td]Table6[/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Reference Tables[/td][/tr][/table]
 
Last edited:
Upvote 0
To use the spreadsheet, the user will first select a category from a dropdown list in column B (B3 in the example). This will dynamically affect the choices presented in a dropdown list in column C. For example: select Fruit in B, you get a list of fruits in C. Change the selection to Vegetable, and the dropdown in C becomes a list of vegetables. This is currently working fine.

The formula I'm trying to build is in columns E, F, G, H, and I. The search criteria is in C, and B determines which sheet (which has a named table, such as Table4) the search is to be completed.
 
Upvote 0
this code should do it for you. I have had to make a few assumptions as to exactly which columns things are in , but hopefully you can debug those if they are not correct;

Code:
Sub gettable()

Dim tbl As ListObject
Dim sht As Worksheet
With Worksheets("Example")
 lastrow = .Cells(Rows.Count, "A").End(xlUp).Row
 Meals = Range(.Cells(1, 1), .Cells(lastrow, 3))
 outarr = Range(.Cells(1, 3), .Cells(lastrow, 9))
End With


With Worksheets("Reference Tables")
 lastref = .Cells(Rows.Count, "A").End(xlUp).Row
 tableref = Range(.Cells(1, 1), .Cells(lastref, 3))
End With


' loop through the meals in Example
 For i = 3 To lastrow
  ' find table for category
    Category = Meals(i, 2)
    For j = 3 To lastref
     ' I assume the list of categories is in column B on the Refence tables sheet
     If Category = tableref(j, 2) Then
      ' we have found the reference to pick up the table
       tablename = tableref(j, 3)
       Exit For
     End If
    Next j
 ' now go and find the table
 ' I assume we can't rely on the Category being exactly the same string as the
 ' the tab name, because it it was it would save searching the workbook for hte table
 
     
'Loop through each sheet and table in the workbook
  tablefnd = False
  For Each sht In ThisWorkbook.Worksheets
    For Each tbl In sht.ListObjects
      tname = tbl.Name
      If tname = tablename Then
      ' we have found the table so load it into a variant array
      table1arr = sht.ListObjects(tablename).Range
      ' now search the table for the ingredient
       For k = 2 To UBound(table1arr)
         If Meals(i, 3) = table1arr(k, 2) Then
          ' we have found the ingredient so copy all the data across
           For mm = 1 To 6
            outarr(i, mm) = table1arr(k, 2 + mm)
           Next mm
         End If
       Next k
         tablefnd = True
         Exit For
       End If
      
    Next tbl
    If tablefnd Then Exit For
  Next sht
 Next i
 With Worksheets("Example")
  Range(.Cells(1, 3), .Cells(lastrow, 9)) = outarr
End With




End Sub
 
Upvote 0
Thanks for continuing to help. I'm trying to work with the code you presented. As I said, it's been a long time since I've messed around with functions and VBA. Having to re-teach myself as I go through it. LOL . I see a couple things that need to be addressed.

' I assume the list of categories is in column B on the Refence tables sheet.
*** No. Column A in the "Reference Tables" sheet is food categories. Column B is the corresponding table for the selected food category.

' I assume we can't rely on the Category being exactly the same string as the
' the tab name, because it it was it would save searching the workbook for hte table
*** Correct. I found that having multiple words in either my sheet name or dropdown list caused problem when trying to run a function. That's why I replaced commas and spaces with an underscore.
 
Upvote 0
OK, I've created the module using your code and attempted to run in a couple times - with not apparent results. I stepped through each line line, observing the results in the Locals Window.

1. It appears that this code is attempting to run against the sheet (a total of 49 rows) rather than simple running against the current row being inputted. My intent is not to input all the categories and food items and then run the macro to populate the nutrients data. I want the nutrients data to get inputted as each individual food item is selected (there's more math that will impact this once I figure out/understand how to pull this off). Then then user will move on to the next row and enter a new item.

2. None of the nutrient data is being populated into the sheet on "Example." I can't figure out why. With 49 interactions, its hard to track where the breakdown is. Or if there is code missing.

3. You'll note there is a row with "Total" in column A. This row automatically totals up the nutrient data in each column. This row, along with the first two, should not be impacted by the macro. In the example I showed, it only included "breakfast." This is what the full sheet looks like:

Unknown 64 bit

[Table="width:, class:head"][tr=bgcolor:#E0E0F0][th] [/th][th]
A
[/th][th]
B
[/th][th]
C
[/th][th]
D
[/th][th]
E
[/th][th]
F
[/th][th]
G
[/th][th]
H
[/th][th]
I
[/th][/tr]
[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
1
[/td][td]
Meal​
[/td][td]
Category​
[/td][td]
Item​
[/td][td]
Qty​
[/td][td]
Carbs​
[/td][td]
Fat​
[/td][td]
Protein​
[/td][td]
Sugar​
[/td][td]
Calories​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
2
[/td][td]
MONDAY​
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][td]
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
3
[/td][td]
Breakfast​
[/td][td]
Meats_Eggs_Nuts​
[/td][td]Egg (large)[/td][td][/td][td]
0.38g​
[/td][td]
4.97g​
[/td][td]
6.29g​
[/td][td]
0.38g​
[/td][td]
74.00g​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
4
[/td][td]
[/td][td]
Milk_Yogurt_Cheese​
[/td][td]Milk (non-fat)[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
5
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
6
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
7
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
8
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
9
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
10
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
11
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
12
[/td][td]Total[/td][td][/td][td][/td][td][/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
13
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
14
[/td][td]
Lunch​
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
15
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
16
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
17
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
18
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
19
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
20
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
21
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
22
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
23
[/td][td]Total[/td][td][/td][td][/td][td][/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
24
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
25
[/td][td]
Snack​
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
26
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
27
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
28
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
29
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
30
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
31
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
32
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
33
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
34
[/td][td]Total[/td][td][/td][td][/td][td][/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
35
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
36
[/td][td]
Dinner​
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
37
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
38
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
39
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
40
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
41
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
42
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
43
[/td][td]
[/td][td]
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
44
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
45
[/td][td]Total[/td][td][/td][td][/td][td][/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
46
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][td=bgcolor:#DBDBDB]
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
47
[/td][td]DAILY TOTALS[/td][td][/td][td][/td][td][/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][td]
0.00g​
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
48
[/td][td]DAILY GOALS[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
49
[/td][td]PROGRESS[/td][td][/td][td][/td][td][/td][td=bgcolor:#FFEB9C]
0.00g
[/td][td=bgcolor:#FFEB9C]
0.00g
[/td][td=bgcolor:#FFEB9C]
0.00g
[/td][td=bgcolor:#FFEB9C]
0.00g
[/td][td=bgcolor:#FFEB9C]
0.00g
[/td][/tr]

[tr=bgcolor:#FFFFFF][td=bgcolor:#E0E0F0]
50
[/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][td][/td][/tr]
[/table]
[Table="width:, class:grid"][tr][td]Sheet: Example[/td][/tr][/table]
 
Upvote 0
No. Column A in the "Reference Tables" sheet is food categories. Column B is the corresponding table for the selected food category.
This will definitely cause it to fail because it won't be able to find any matches. You need to change the following two lines of code to pick columns a and b
Code:
    ' I assume the list of categories is in column A on the Refence tables sheet    
     If Category = tableref(j, 1) Then
      ' we have found the reference to pick up the table
       tablename = tableref(j, 2)
I tend to use number references for columns all the time becaue it is much easier to index through the rows and columns using numbers particularly in variant arrays where letter references for columns won't work

Can you make this change and try the code as it is to see whether I have got the "lookup" coding correct, before we try to modify it to your exact requirements. (Don't worry about the totals bit we will deal with that later)
To change the code to update the ingredients as the user enters the data can be done in two ways.
1: Put some code into the worksheet change event subroutine which will do the lookup
2: create a special user defined function to do the lookup
Both of these could end up making the workbook rather slow. the main reason this routine is slow is because of needing to search through the whole workbook to find which table to use. this will be done multiple times with either of the two soluitions while the user is entering data, so speed is of importance
Thus it would be much better if you could add the name of the worksheet which each table is on into the Reference table. This could be added as a third column. Is this possible?
 
Last edited:
Upvote 0
I don't know , I have never needed to do that, probably because I am usually just posting code
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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