excel - 1004 错误:无法获取 WorksheetFunction 类的 StDev_S 属性
问题描述
这是我正在使用的电子表格数据。
https://docs.google.com/spreadsheets/d/1aEgf29yigbwbemJdToJrlws9iJFMamUztM0PbdrsOQw/edit?usp=sharing
它突出显示了这条线
.Cells(4, i).Value = Application.WorksheetFunction.StDev_S(rng)
,我不知道为什么。我打印了范围,所有值都是我想要的。我过去用范围做过类似的工作表函数,没有遇到问题。
这是我正在使用的代码
Sub run_stats()
Dim i As Long
Dim nrows As Long
Dim rng As Range
Dim cell As Range
For i = 3 To 50
Set rng = Worksheets("Sheet1").Range(Worksheets("Sheet1").Cells(11, i), Worksheets("Sheet1").Cells(11, i).End(xlDown))
For Each cell In rng
Debug.Print cell.Value
Next cell
With Worksheets("Statistics")
.Cells(4, i).Value = Application.WorksheetFunction.StDev_S(rng)
.Cells(5, i).Value = Application.WorksheetFunction.Average(rng) + (2 * Application.WorksheetFunction.StDev_S(rng))
.Cells(6, i).Value = Application.WorksheetFunction.Average(rng) + Application.WorksheetFunction.StDev_S(rng)
.Cells(7, i).Value = Application.WorksheetFunction.Average(rng)
.Cells(8, i).Value = Application.WorksheetFunction.Average(rng) - Application.WorksheetFunction.StDev_S(rng)
.Cells(9, i).Value = Application.WorksheetFunction.Average(rng) - (2 * Application.WorksheetFunction.StDev_S(rng))
.Cells(10, i).Value = Application.WorksheetFunction.Min(rng)
.Cells(11, i).Value = Application.WorksheetFunction.Max(rng)
End With
Next i
End Sub
编辑:更正的代码
Sub run_stats()
Application.ScreenUpdating = False
Application.Calculation = xlManual
Dim i As Long
Dim nrows As Long
Dim rng As Range
Dim cell As Range
Worksheet("Statistics").Range("C4:AX35").ClearContents
For i = 3 To 50
Set rng = Worksheets("Sheet1").Range(Worksheets("Sheet1").Cells(11, i), Worksheets("Sheet1").Cells(11, i).End(xlDown))
For Each cell In rng
If Application.WorksheetFunction.IsError(cell) Then
cell.Value = ""
End If
Next cell
If Application.WorksheetFunction.CountA(rng) >= 2 Then
With Worksheets("Statistics")
.Cells(4, i).Value = Application.WorksheetFunction.StDev_S(rng)
.Cells(5, i).Value = Application.WorksheetFunction.Average(rng) + (2 * Application.WorksheetFunction.StDev_S(rng))
.Cells(6, i).Value = Application.WorksheetFunction.Average(rng) + Application.WorksheetFunction.StDev_S(rng)
.Cells(7, i).Value = Application.WorksheetFunction.Average(rng)
.Cells(8, i).Value = Application.WorksheetFunction.Average(rng) - Application.WorksheetFunction.StDev_S(rng)
.Cells(9, i).Value = Application.WorksheetFunction.Average(rng) - (2 * Application.WorksheetFunction.StDev_S(rng))
.Cells(10, i).Value = Application.WorksheetFunction.Min(rng)
.Cells(11, i).Value = Application.WorksheetFunction.Max(rng)
End With
End If
Next i
Application.ScreenUpdating = True
Application.Calculation = xlAutomatic
End Sub
解决方案
检查 Sheet1 中的数据以获取错误值,例如 #N/A。您可以尝试将错误值更改为空字符串的解决方法:
Option Explicit
Sub run_stats()
Dim i As Long
Dim nrows As Long
Dim rng As Range
Dim cell As Range
For i = 3 To 50
Set rng = Worksheets("Sheet1").Range(Worksheets("Sheet1").Cells(11, i), Worksheets("Sheet1").Cells(11, i).End(xlDown))
For Each cell In rng
Debug.Print cell.Value
'check if value is error -> change to empty cell if it is
If Application.WorksheetFunction.IsError(cell) Then
cell.Value = ""
End If
Next cell
With Worksheets("Statistics")
.Cells(4, i).Value = Application.WorksheetFunction.StDev_S(rng)
.Cells(5, i).Value = Application.WorksheetFunction.Average(rng) + (2 * Application.WorksheetFunction.StDev_S(rng))
.Cells(6, i).Value = Application.WorksheetFunction.Average(rng) + Application.WorksheetFunction.StDev_S(rng)
.Cells(7, i).Value = Application.WorksheetFunction.Average(rng)
.Cells(8, i).Value = Application.WorksheetFunction.Average(rng) - Application.WorksheetFunction.StDev_S(rng)
.Cells(9, i).Value = Application.WorksheetFunction.Average(rng) - (2 * Application.WorksheetFunction.StDev_S(rng))
.Cells(10, i).Value = Application.WorksheetFunction.Min(rng)
.Cells(11, i).Value = Application.WorksheetFunction.Max(rng)
End With
Next i
End Sub
推荐阅读
- reactjs - 更改数组状态中的状态 redux 反应原生
- java - SpringBoot 2.1.x 中使用 RestTemplateBuilder 的 requestFactory 自定义 RestTemplate 不向后兼容 1.5.x 版本
- asynchronous - 如何测试 initState() 中有异步调用的 Flutter 应用程序?
- python - matplotlib 标注 竖线标注
- excel - 我收到语法错误,你能检查我的代码吗?
- c - 将 *C.uint8_t 转换为 []uint8 是否安全?
- javascript - Firebase query with React Native not showing on my screen (but showing on console.log)
- c++ - 如何在 crypto++ 中使用 SocketSource 和 SocketSink
- postgresql - Postgres 10.4 中的物化视图不刷新
- javascript - addClass 不更新 CSS