如何写一个宏来对空白行之前的数据列做标准偏差?

时间:2016-06-17 22:12:14

标签: excel vba stdev

enter image description here

我已经设法通过编写这个宏来解决问题

'This works out how many rows (another way)
Dim AV As Long, AVC As Long
AV = Range("O" & Rows.Count).End(xlUp).Row
AVC = AV - 1

 ' This adds up all the data starting from row 2 to before the blank row
 Dim LR As Long
 myRange = ActiveSheet.Range("O2", Range("O2").End(xlDown))
 LR = WorksheetFunction.sum(myRange)
'MsgBox LR

 ' So finding the mean
 Dim mean As Long
 Range("P2").Select
 ActiveCell.FormulaR1C1 = LR / AVC
 Selection.Value = Format(ActiveCell, "#.00")

,其在细胞P2中得到17.84的结果

但是当我试图写相同来计算标准偏差时,它返回的值为0.这是否意味着我不能使用下面的类似编码来完成这项工作?

 ' Doing stdev on range
 Dim MD As Long
 myRange = ActiveSheet.Range("O2", Range("O2").End(xlDown))
 MD = WorksheetFunction.StDev_S(myRange)
 MsgBox MD

另外,我无法弄清楚如何复制和粘贴它并根据O列中的行数停止。真的很感激一些帮助,因为我对编写宏非常陌生。

enter image description here

1 个答案:

答案 0 :(得分:0)

dim MD as Long更改为dim MD as double