如何编写一个宏来对空白行之前的一列数据进行标准偏差?
How to write a macro to do standard deviation on a column of data just before a blank row?
我已经通过编写这个宏设法计算出均值
'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 列中的行数停止。非常感谢您的帮助,因为我对编写宏还很陌生。
将 dim MD as Long
更改为 dim MD as double
。
我已经通过编写这个宏设法计算出均值
'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 列中的行数停止。非常感谢您的帮助,因为我对编写宏还很陌生。
将 dim MD as Long
更改为 dim MD as double
。