Excel VBA平均最后15个非空白单元

时间:2015-11-04 14:00:55

标签: excel-vba excel-formula vba excel

我正在使用下面的公式从VBA进入一个单元格,它工作正常,直到我在我计算的单元格中有一个公式,我留空,直到输入其他数据。如何让这个公式平均最后15个单元格并忽略包含公式的空白单元格?

=AVERAGE(OFFSET(AE8,COUNTA(AE:AE)-16,0,16,1))

1 个答案:

答案 0 :(得分:2)

我写了一个UDF并在工作表中引用它,这会显示一条消息,如果它找不到平均15个数字,你可以调整它找到它找到的所有数字的平均值,最多为15,或类似的东西

Function Average15(rng As Range) 'take entire column as argument
ColumnNumber = rng.Column
rownumber = Cells(65536, ColumnNumber).End(xlUp).Row
If rownumber < 15 Then
    Average15 = "Not Enough Cells"
    Exit Function
End If
Counter = 0
While Counter < 15
    If rownumber = 1 Then 'assumes no header row(s), change this to the first row with data in it
        Average15 = "Not enough values"
        Exit Function
    End If
    If Cells(rownumber, ColumnNumber).Value <> "" Then
        Counter = Counter + 1
        Totals = Totals + Cells(rownumber, ColumnNumber).Value
    End If
    rownumber = rownumber - 1
Wend
Average15 = Totals / 15
End Function
表格中的

是公式

=AVERAGE15(A:A)
相关问题