You could use the criteria for filtering with SUMIFS.