Если вы не против использования VBA, эта функция будет делать то, что вы хотите.
=Average_Orders_Subset(range, criteria)
Я изменил это в своем исходном ответе, чтобы улучшить функциональность, и добавил комментарии, чтобы Вы можете видеть, что он делает. С помощью функции вы можете добавить столько заказов, сколько хотите. Я также проверил его, и он будет работать, если буквы заказа будут заменены на числа, т.е. a = 1, b = 2 и т. Д. ... Надеюсь, это поможет. Не стесняйтесь спрашивать, если у вас есть какие-либо вопросы.
Если ваши данные начинаются в ячейке A1, они работают следующим образом: Пример: =Average_Orders_Subset(A1:C13,"Chicken")
С этой версией вы можете рассчитывать на курицу, рис, хлеб, что угодно. Если вы не знаете, , вот , как добавить код в рабочую книгу. Если хотите, я могу выслать вам копию рабочей книги с уже встроенной книгой.
Option Explicit
' User Defined Function to Average totals for only orders that contain specific string
' Pass the range and string you are looking for to the function
' Note the criteria string is case senstive so "chicken" is not the same as "Chicken"
'
' Example: =Average_Orders_Subset(A1:C13,"Chicken")
'
Public Function Average_Orders_Subset(rng As Range, criteria As String) As Double
Dim ws As Worksheet
Dim arrData() As Variant
Dim arrOrder() As Variant
Dim i As Long
Dim j As Long
Dim cnt As Long
Dim sum As Double
Dim avg As Double
' The worksheet with data
Set ws = ThisWorkbook.ActiveSheet
' Counter and array to keep track of order letters
cnt = 0
ReDim arrOrder(cnt)
With ws
' Create an array with all the values
arrData = rng.Value2
' Iterate through the array looking for orders with the criteria e.g., "Chicken"
For i = 2 To UBound(arrData)
' If criteria is found
If arrData(i, 2) = criteria Then
If cnt > 0 Then
' If the array of order letters is less than 0
If arrData(i, 1) <> arrOrder(cnt - 1) Then
' Checking if the order letter is already in the array so orders with two Chicken
' or multipe of any criteria don't get double counted
' Add them to the order letter array
arrOrder(cnt) = arrData(i, 1)
cnt = cnt + 1
ReDim Preserve arrOrder(cnt)
End If
ElseIf cnt = 0 Then
' This is to add the first occurence of the critera to the order array
arrOrder(cnt) = arrData(i, 1)
cnt = cnt + 1
ReDim Preserve arrOrder(cnt)
End If
End If
Next i
' Remove the last empty value in the order array
' this is a result of the count expanding the array after the value is added
ReDim Preserve arrOrder(UBound(arrOrder) - 1)
' Reset counter
cnt = 0
sum = 0
' For all the values in the order array
For i = LBound(arrOrder) To UBound(arrOrder)
' For all the values in the range
For j = 2 To UBound(arrData)
' If a row in the range matches the order letter add that value to the sum
If arrData(j, 1) = arrOrder(i) Then
sum = sum + arrData(j, 3)
' Keep track of the number of summed values for an average
cnt = cnt + 1
End If
Next j
Next i
' Calculte the average
avg = (sum / cnt)
' Pass the average back to the formula
Average_Orders_Subset = avg
End With
Set rng = Nothing
Set ws = Nothing
End Function