in general when using an averageif you don't use the equal sign. it is more like this... =AVERAGEIF(AA4:BL43,F4:F43,"D")
But that will not work over a range. Maybe to get a better formula you can upload a workbook with sample data.