Reputation: 4650
Here's my sheet setup:
item ref
. And from that I want to vlookup
or something, to fetch the "Cost" of all those items in the list, and SUM them up.Example:
I was able to do that for only 1 item, but I want the formula to work on the "list" of items in B9.
I even tried with this query formula, I just didn't know how to say "matching any item from the list", which will be a string in another cell:
=SUM(QUERY(A:B,"select B where A matches 'ID-100'"))
Upvotes: 1
Views: 462
Reputation:
If your "items used" column consistently uses comma-space separated format for item lists, the following will work.
=query(Cost!A:B, "select sum(B) where A = '" & join("' or A = '", split(B9, ", ")) & "' label sum(B) ''")
Here, split
splits the list of items used into individual item Ids. These are then joined by putting ' or A = '
in between, for example:
ID-101' or A = 'ID-101
There are bits prepended and appended to form the query string, e.g.,
select sum(B) where A = 'ID-101' or A = 'ID-101' label sum(B) ''
which does the job. The label part is necessary to get one-cell output, without a "Sum(Cost)" header getting in the way.
A detail concerning split: as written above, split(B9, ", ")
splits by either comma or space (ignoring empty string in the output). So it will work even with ID-1,ID-2, ID-3 ID-4
. On the other hand, this may be a problem if your IDs contain spaces. The stricter splitting mode is split(B9, ", ", False)
which requires the whole string ", " to be used as a separator.
Upvotes: 1