如何在不同的单元格中使用平均值功能?

时间:2019-06-14 21:38:11

标签: excel vba

我将使用平均值函数从不同的列(B:D)获取平均值(我将空白行插入到要显示平均值的位置。)

例如: 之前:

ColA ColB   ColC   ColD
 1.1  1      6      1
 1.1  2      5      2
 1.2  3      4      3
 1.2  4      3      4
 1.3  5      2      5
 1.3  6      1      6

然后,我像这样插入空白行:

ColA ColB   ColC   ColD
 1.1  1      6      1
 1.1  2      5      2

 1.2  3      4      3
 1.2  4      3      4

 1.3  5      2      5
 1.3  6      1      6

我从这里学习了如何插入空白行:enter link description here

我需要的是

ColA ColB   ColC   ColD
 1.1  1      6      1
 1.1  2      5      2
     1.5    5.5    1.5
 1.2  3      4      3
 1.2  4      3      4
     3.5    3.5    3.5
 1.3  5      2      5
 1.3  6      1      6

我现在所拥有的:

Dim lastr As Long
lastr = Range("A" & rows.Count).End(xlUp).Row

Range("B1:B" & lastr).SpecialCells(xlCellTypeBlanks).Select
Selection.Formula = "Average" 'I don't know how to use the Average function in multiple cells here.

我将用“宏”记录公式替换“平均”,但这对我不起作用。

如何获得此处的平均值?非常感谢。

1 个答案:

答案 0 :(得分:1)

Sub TT()
    Dim lastr As Long
    lastr = Range("A" & Rows.Count).End(xlUp).Row
    'if you enter the first AVERAGE then the others will adjust as needed
    Range("B1:D" & lastr).SpecialCells(xlCellTypeBlanks).Formula = "=AVERAGE(B1:B2)"
End Sub