用Excel VBA編寫程式去計Mean/Average

2008-04-09 7:14 am
請問點樣用Excel VBA寫程式去計mean/average?

我會一路有data加落去, 所以想用VBA o既procedure, 一run就可以係excel table 最後一行計到mean/average. (即如果有20行data, mean/average就係21行計出黎)

我諗住要用usedrange.rows.count, 但係諗唔到中間點寫, 請幫幫忙

唔該

回答 (2)

2008-04-09 4:19 pm
✔ 最佳答案
Assume u put ur data in column A, starting from cell A1

you can use this macro


***************************************************
Sub InsertAverage()

i = 1
Do While Application.WorksheetFunction.CountBlank(Cells(i, 1)) = 0
i = i 1
Loop

Cells(i, 1).Select

ActiveCell.FormulaR1C1 = "=AVERAGE(R[-" & i - 1 & "]C:R[-1]C)"

End Sub

***************************************************

Then it will automatically add =AVERAGE function after the last row.





p.s. look at 參考資料 for the [ActiveCell.FormulaR1C1] line, it cant display correct above
參考: ActiveCell.FormulaR1C1 = ”=AVERAGE(R[-” & i - 1 & ”]C:R[-1]C)”
2008-04-09 10:33 pm
判斷單一Column, 不應用 UsedRange
此題應先固定計算區內第一個位置, 假設是 A3
再用VBA判斷 Column A 的最後一個資料的位置
再在最後一個資料位置的下一格輸入公式
Sub fx_write()
Set x = [A65536].End(xlUp)
xx = Replace(x.Address, "$", "")
x.Offset(1, 0) = "=AVERAGE(A3:" & xx & ")"
End Sub
如不顯示公式, 只顯示常值, 在最後一句加上:
x.Offset(1, 0) = x.Offset(1, 0)


收錄日期: 2021-04-24 22:41:29
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20080408000051KK03161

檢視 Wayback Machine 備份