需要帮助在函数中循环遍历单元格

时间:2015-05-01 15:27:55

标签: excel vba excel-vba

我想循环遍历整个列W2,同时取每行的平均值。该列长1326。

我在下面的代码只计算W2B2的平均值。有人可以帮忙吗

NumCellsFromEdge = 3
Dim NumOfCells As Integer
NumOfCells = 3

MsgBox GetTopDrTS(NumCellsFromEdge, NumOfCells) & " = average of " & NumCellsFromEdge & " cells in, and " & NumOfCells & " cells wide"

End Sub

Function GetTopDrTS(L_NumCellsFromEdge As Integer, L_NumOfCells As Integer) As Double
Dim val As Double
Dim mycol As Double
val = 0
mycol = 23

'Range("W2").Select
'Columns(mycol).Select

Dim i As Integer
For i = 0 To L_NumOfCells - 1
Range("W2" & i).Select

    val = val + Selection.Worksheet.Cells(Selection.Row, Selection.Column + EdgePos + L_NumCellsFromEdge + i).Value

Next i

GetTopDrTS = val / L_NumOfCells

End Function

2 个答案:

答案 0 :(得分:0)

这就是你所需要的:

MsgBox [average(w:w)]

答案 1 :(得分:0)

Application.average(片材(1).range( “E:E”))

会告诉您表格1中E列的平均值