简体   繁体   中英

Sum values from another sheet based on a set of IDs

I want to create a workbook that would help people log their eating habits. The workbook currently has two sheets, "Recipes" and "Diary".

Recipes has columns ID, NAME, KCAL and PROTEIN. The user can add any recipes they frequently use, and just refer to them using the numeric ID.

Diary has columns DATE, TOTAL_KCAL and TOTAL_PROTEIN. The idea is for each date the user could see the total amount of energy and protein eaten.

I have already figured out how to get the kcal and protein amounts of a single recipe from the recipes sheet into the diary sheet using VLOOKUP():

=VLOOKUP(1;Table1;3;FALSE) //For kcal

but I really would like to automate this a bit. At this stage if I want to use this sheet I need to copy the whole function (and add a + prefix), paste it at the end and change the first parameter to the correct ID. Then I have to repeat this process also to the TOTAL_PROTEINS column (where I need to remember to change the source parameter from 3 to 4).

I guess the optimal situation would be if the Diary sheet also had some EATEN_RECIPES column, where people could add a list of IDs (1,15,6) but I have no idea if this is possible in excel. The point is, I want to be able to write down an ID to diary and have excel automatically fetch the kcal and protein amounts from the recipes sheet and update the total amount in the diary.

This is probably quite a simple matter, but I have no idea what search words to use in google, any comments on this are appreciated. (Also I need a better title.)

If I understand what you are trying to do correctly, you can address your copy & paste issue by referring to the data in another sheet by prefixing the data you want to acquire with !. For instance, if your data is in Sheet1, in the range of A1:A7, you could refer to it in your formulas and functions with:

Sheet1!A1:A7

As for the list of IDs... You could try this as a starting point:

http://www.ozgrid.com/forum/showthread.php?t=86497

It gets into custom coding... you could also just have the user enter each id into a different column, but that's an ugly hack.

BTW, it sounds like what you are doing is getting close to database territory... Very often Excel sheets end up being their own mini-databases. If you're comfortable with databases, and don't mind the work, you could look into using a database instead.

This is the function I ended up with. Based on the URL provided by user1161318. I know it's not that pretty, but I just wanted to get it to work and I'm new to Visual Basic.

Function MultiVLookup2(LookUpVal, LookUpRng As Range, LookUpCol As Long)

Dim v, w, i, rng As Range, sum As Single

v = Split(LookUpVal, ",")
sum = 0

ReDim w(UBound(v, 1))

For i = LBound(v, 1) To UBound(v, 1)
    w(i) = WorksheetFunction.VLookup(Val(v(i)), LookUpRng, LookUpCol, False)
Next i

For i = LBound(v, 1) To UBound(v, 1)
    sum = sum + w(i)
Next i

MultiVLookup2 = Round(sum, 1)

End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM