下面是一个有效的解决方案:
Sub add_averages()
Dim r As Long, c As Long, right_c As Long
r = 2
Do While Not IsEmpty(Range("a" & r))
c = 2
'find first non-empty cell in row
Do While IsEmpty(Cells(r, c)) And c <= 6
c = c + 1
Loop
If c <= 6 Then
dest_c = 7
right_c = c + 2
If right_c > 6 Then right_c = 6
Cells(r, dest_c).Value = average_cells(r, c, right_c)
If right_c < 6 Then
dest_c = dest_c + 1
c = c + 3
right_c = c + 2
If right_c > 6 Then right_c = 6
Cells(r, dest_c).Value = average_cells(r, c, right_c)
End If
End If
r = r + 1
Loop
End Sub
Function average_cells(r As Long, left_c As Long, right_c As Long) As Single
'handles up to three numbers
If left_c = right_c Then
average_cells = Cells(r, left_c).Value
ElseIf left_c + 1 = right_c Then
average_cells = (Cells(r, left_c).Value + Cells(r, right_c).Value)/2
Else
average_cells = (Cells(r, left_c).Value + Cells(r, left_c + 1).Value + Cells(r, right_c).Value)/3
End If
End Function
它假定您希望把列所得的平均值G和H参照列数字,我不能完全利用现有的平均函数的方法我想,所以我写了我自己的。
希望这有助于〜
你可以把图片的链接在你的问题虽然,有人可以一起去,并把它正确地在你的问题。每个名字的结果都是在不同的月份结束吗? – Jerry
它只覆盖1年吗?你想要什么平均值?你想避免宏吗? –
一旦有开始的月份,以后的所有月份都会有值,第一个非空白值后没有空白? – user2140261