下面是一個有效的解決方案:
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