This table helps populate a meal plan with the calorie allotment per food type and meal, factored against the food’s calorie density (i.e. how many calories per gram etc.). But sometimes the food is in units not grams. For instance an egg is 60 calories of fat. So if the allotment is 106 as in the above case the formula rounds it off to 2 eggs and reports the excess (120-106=14 calories), which need to be deducted from another meals allotment for fat.
So I set out to find another meal with the max calories so the 14 aren’t missed. Hence the Largest function. When the cell address is returned I need to check that it isn’t also in units as this would not accomplish anything (and in particular not the same cell that generated the excess, as occurs in this case).
I am including the meal plan table and a snapshot of the code I am using. I don’t know how much of this will still be functional when you get it but hopefully your ?? are answered.
[TABLE="width: 607"]
<colgroup><col><col><col><col><col><col><col span="3"></colgroup><tbody>[TR]
[TD]Meal no.[/TD]
[TD] [/TD]
[TD] [/TD]
[TD] [/TD]
[TD]Food type[/TD]
[TD]Amount[/TD]
[TD]unit[/TD]
[TD="colspan: 2"]Unit cal excess/short[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]MEAL 1[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Protein[/TD]
[TD="align: right"]4[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Whey Protein Powder[/TD]
[TD]35[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Carbs[/TD]
[TD="align: right"]3.33[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Rolled Oats[/TD]
[TD]107[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fats[/TD]
[TD]60[/TD]
[TD]per[/TD]
[TD]unit[/TD]
[TD]Whole Eggs[/TD]
[TD]2[/TD]
[TD]units[/TD]
[TD="align: right"]14[/TD]
[TD]cal excess/short[/TD]
[/TR]
[TR]
[TD]Fruit/Veggies[/TD]
[TD="align: right"]0.47[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Mixed Berries[/TD]
[TD]227[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]MEAL 2[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Protein[/TD]
[TD="align: right"]1.5[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Lean Minced Beef[/TD]
[TD]208[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Carbs[/TD]
[TD="align: right"]1.2[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Basmati Rice[/TD]
[TD]140[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fats[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]N/A[/TD]
[TD]N/A[/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fruit/Veggies[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]N/A[/TD]
[TD]N/A[/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]MEAL 3[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Protein[/TD]
[TD="align: right"]1.2[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Chicken Breast[/TD]
[TD]237[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Carbs[/TD]
[TD="align: right"]0.75[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Kumera[/TD]
[TD]379[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fats[/TD]
[TD="align: right"]2.3[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Avocado[/TD]
[TD]46[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fruit/Veggies[/TD]
[TD="align: right"]0.3[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Approved Vegetables[/TD]
[TD]118[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]MEAL 4[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Protein[/TD]
[TD="align: right"]1.2[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Chicken Breast[/TD]
[TD]260[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Carbs[/TD]
[TD="align: right"]0.75[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Kumera[/TD]
[TD]160[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fats[/TD]
[TD="align: right"]2.3[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Avocado[/TD]
[TD]21[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fruit/Veggies[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]N/A[/TD]
[TD]N/A[/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]MEAL 5[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Protein[/TD]
[TD="align: right"]1.2[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Chicken Breast[/TD]
[TD]414[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Carbs[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]N/A[/TD]
[TD]N/A[/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fats[/TD]
[TD="align: right"]8[/TD]
[TD]per[/TD]
[TD]ml[/TD]
[TD]Lite Olive Oil[/TD]
[TD]13[/TD]
[TD]mls[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fruit/Veggies[/TD]
[TD="align: right"]0.3[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Approved Vegetables[/TD]
[TD]355[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]MEAL 6[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Protein[/TD]
[TD="align: right"]4[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Whey Protein Powder[/TD]
[TD]46[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Carbs[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]N/A[/TD]
[TD]N/A[/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fats[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]N/A[/TD]
[TD]N/A[/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fruit/Veggies[/TD]
[TD="align: right"]0.47[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Mixed Berries[/TD]
[TD]98[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]MEAL 7[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Protein[/TD]
[TD="align: right"]4[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Whey Protein Powder[/TD]
[TD]29[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Carbs[/TD]
[TD="align: right"]1[/TD]
[TD]per[/TD]
[TD]gram[/TD]
[TD]Banana[/TD]
[TD]115[/TD]
[TD]grams[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fats[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]N/A[/TD]
[TD]N/A[/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD]Fruit/Veggies[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]N/A[/TD]
[TD]N/A[/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD]Meal adjusted[/TD]
[TD]Adjusted allotment [/TD]
[TD][/TD]
[TD]TDC cell[/TD]
[TD] [/TD]
[TD] [/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD="align: right"]0[/TD]
[TD]Total excess/shortage Protein[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD="align: right"]0[/TD]
[TD]Total excess/shortage Protein[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD="align: right"]14[/TD]
[TD]Total excess/shortage Fats[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD="align: right"]0[/TD]
[TD]Total excess/shortage Fruits/Veggies[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[TD="align: left"]
[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD] [/TD]
[TD] [/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
Sub ReassignExcessCals()
Application.ScreenUpdating = False
Dim units As String
Dim wkb As Workbook
Dim ws As Worksheet
Dim mealname As String
Dim quantity As String
Set wkb = ThisWorkbook
k = 7
l = "G"
Sheet22.Activate
Range("A47:G54").Select
Range("G54").Activate
Selection.ClearContents
Range("g47").Select
For j = 1 To 4
If Abs(activecell.Offset(0, 1)) >= 10 Then
For i = 1 To 4
activecell.Offset(4, 1).Value = i
If activecell.Offset(, 1) >= 0 Then
activecell.FormulaArray = _
"=CELL(""address"",OFFSET(TDC!R[-38]C[-5],0,MATCH(LARGE(TDC!R[-38]C[-5]:TDC!R[-38]C[-4]:TDC!R[-38]C[-3]:TDC!R[-38]C[-2]:TDC!R[-38]C[-1]:TDC!R[-38]C[0]:TDC!R[-38]C[1],R[4]C[1]),TDC!R[-38]C[-5]:R[-38]C[1],0)-1))"
'
in the above line I am listing each cell separately in lieu of an array which I hoped that after testing if it was a unit it could be eliminated from the search.
Else
activecell.FormulaArray = _
"=CELL(""address"",OFFSET(TDC!R[-38]C[-5],0,MATCH(SMALL(IF(TDC!R[-38]C[-5]:R[-38]C[1]>0,(TDC!R[-38]C[-5]:R[-38]C[1])),R[4]C[1]),TDC!R[-38]C[-5]:R[-38]C[1],0)-1))"
End If
'Find its Cell Address
Call CellAddress(j, k, l)
'Qualify quantity type
activecell.Offset(4, 0).Value = "=INDIRECT(R[-4]C[-1])"
If activecell.Offset(4, 0).Value <> "units" Then
Exit For
Else
End If
Next i
End If
Cells(47 + j, 7).Select
Next j
End Sub