1004错误:无法获取WorksheetFunction类的StDev_S属性

时间:2018-11-15 16:10:31

标签: excel vba excel-vba

这是我正在使用的电子表格数据。

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

1 个答案:

答案 0 :(得分:1)

检查Sheet1中的数据是否有错误值,例如#N / A。您可以尝试以下将错误值更改为空字符串的解决方法:

var bArr = $(".but")
var paises = [
  {country: "India", growth: 35},
  {country: "Indonesia",growth: 30},
  {country: "Russia",growth: 25},
  {country: "Mars",growth: 20},
  {country: "Pluton",growth: 15}    
]

for (i = 0; i< bArr.length; i++){
  $(bArr[i]).click(function(){
    $("#par").html(paises[i].country)
  })
}