vba - 如何计算列的平均值,然后自动填充同一列

时间:2017-08-04 09:48:58

标签: excel vba excel-vba

如何计算一系列单元格的平均值(来自H6:lastrow),在I6中填充该数字并使用excel VBA将相同数字自动填充到最后一行(I6:lastrow)?希望自动完成这些步骤。

到目前为止我所拥有的(但不起作用)

Sub AverageRates()

With ActiveSheet
    'Determine last row
    Dim lastRow As Long
    lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

    'Average rate calculation
    Range("I6:I" & lastRow).Value = Application.WorksheetFunction.Average("H6:H" & lastRow)


End With

End Sub 

非常感谢!

2 个答案:

答案 0 :(得分:0)

'I6'中有以下公式:=AVERAGE(H6:<whatever your lastrow is>)。现在,您只需添加对每行中单元格的引用(即=I6

答案 1 :(得分:0)

Code would be like bellow.

Sub AverageRates()

With ActiveSheet
    'Determine last row
    Dim lastRow As Long
    Dim myAvg As Double
    lastRow = .Cells(.Rows.Count, "F").End(xlUp).Row

    'Average rate calculation
    myAvg = Application.WorksheetFunction.Average(Range("H6:H" & lastRow))
    Range("I6:I" & lastRow).Value = myAvg


End With

End Sub