2017-08-27 299 views
2

我得到了运行时错误424的错误:所需的对象。我被困在创建一个公式来计算标准偏差。我想我通过定义范围类型来做错了什么。有什么建议么?VBA - 运行时错误424:所需的对象

Sub result() 
    ' I can see the average 
    MsgBox Application.Average(getRangeByYear(2, year)) 
    ' Error is caused in here 
    MsgBox sdExcludesZero(getRangeByYear(2, year)) 
End sub 

Function meanExcludesZero(r As Excel.Range) 
    Dim count As Double 
    Dim sum As Double 
    For Each cell In r 
     If cell.Value <> 0 Then 
      count = count + 1 
      sum = sum + cell.Value 
     End If 
    Next cell 
    meanExcludesZero = sum/count 
End Function 

Function sdExcludesZero(r As Excel.Range) 
    Dim mean As Double 
    mean = meanExcludesZero(r) 
    Dim sumOfSquareDiff As Double, count As Double 
    For Each cell In r 
     If cell.Value <> 0 Then 
      count = count + 1 
      sumOfSquareDiff = sumOfSquareDiff + (cell.Value - mean) * (cell.Value - mean) 
     End If 
    Next cell 
    sdExcludesZero = Application.sqrt(sumOfSquareDiff/count) 
End Function 

Function getRangeByYear(column As Integer, year As Integer) 
    ... 
    ... 
    getRangeByYear = Range(Cells(startIndex, column), Cells(endIndex, column)) 
End Function 

回答

2

代码存在一些问题。

  1. getRangeByYear返回Variant而传递给sdExcludesZero参数是Excel.Range(相应地,cell.Value改变cell
  2. 代替Application.sqrt使用Sqr

见下面的代码。

Sub result() 
    ' I can see the average 
    MsgBox Application.Average(getRangeByYear(2, year)) 
    ' Error is caused in here 
    MsgBox sdExcludesZero(getRangeByYear(2, year)) 
End Sub 

'Function meanExcludesZero(r As Excel.Range) 
Function meanExcludesZero(r As Variant) 
    Dim count As Double 
    Dim sum As Double 
    For Each cell In r 
     'If cell.Value <> 0 Then 
     If cell <> 0 Then 
      count = count + 1 
      'sum = sum + cell.Value 
      sum = sum + cell 
     End If 
    Next cell 
    meanExcludesZero = sum/count 
End Function 

'Function sdExcludesZero(r As Excel.Range) 
Function sdExcludesZero(r As Variant) 
    Dim mean As Double 
    mean = meanExcludesZero(r) 
    Dim sumOfSquareDiff As Double, count As Double 
    For Each cell In r 
     'If cell.Value <> 0 Then 
     If cell <> 0 Then 
      count = count + 1 
      'sumOfSquareDiff = sumOfSquareDiff + (cell.Value - mean) * (cell.Value - mean) 
      sumOfSquareDiff = sumOfSquareDiff + (cell - mean) * (cell - mean) 
     End If 
    Next cell 
    'sdExcludesZero = Application.sqrt(sumOfSquareDiff/count) 
    sdExcludesZero = Sqr(sumOfSquareDiff/count) 
End Function 

Function getRangeByYear(column As Integer, year As Integer) 
    '... 
    '... 
    getRangeByYear = Range(Cells(startIndex, column), Cells(endIndex, column)) 
End Function 

我已评论需要更改的行,并在其下面添加新行。如果有什么不清楚,请告诉我。

意见建议:而不是cell使用任何其他变量名称。


编辑:你只需要的功能getRangeByYear返回类型更改为Range。因此使用的,

Set getRangeByYear = Range(Cells(startIndex, column), Cells(endIndex, column)) 

代替

getRangeByYear = Range(Cells(startIndex, column), Cells(endIndex, column)) 

另一个变化将与

sdExcludesZero = Sqr(sumOfSquareDiff/count) 

更换

sdExcludesZero = Application.sqrt(sumOfSquareDiff/count) 

请参见下面的完整代码。

Sub result() 
    ' I can see the average 
    MsgBox Application.Average(getRangeByYear(2, year)) 
    ' Error is caused in here 
    MsgBox sdExcludesZero(getRangeByYear(2, year)) 
End Sub 

Function meanExcludesZero(r As Excel.Range) 
    Dim count As Double 
    Dim sum As Double 
    For Each cell In r 
     If cell.Value <> 0 Then 
      count = count + 1 
      sum = sum + cell.Value 
     End If 
    Next cell 
    meanExcludesZero = sum/count 
End Function 

Function sdExcludesZero(r As Excel.Range) 
    Dim mean As Double 
    mean = meanExcludesZero(r) 
    Dim sumOfSquareDiff As Double, count As Double 
    For Each cell In r 
     If cell.Value <> 0 Then 
      count = count + 1 
      sumOfSquareDiff = sumOfSquareDiff + (cell.Value - mean) * (cell.Value - mean) 
     End If 
    Next cell 
    'sdExcludesZero = Application.sqrt(sumOfSquareDiff/count) 
    sdExcludesZero = Sqr(sumOfSquareDiff/count) 
End Function 

Function getRangeByYear(column As Integer, year As Integer) As Range 
    '... 
    '... 
    Set getRangeByYear = Range(Cells(startIndex, column), Cells(endIndex, column)) 
End Function 
+0

非常感谢。现在很清楚。对范围和变种的类型感到困惑 –

+0

@PakHoCheung - 你只需要改变函数'getRangeByYear'的返回类型。请参阅编辑答案。 – Mrig

+0

使用set有什么区别?这是否像内存分配? –

相关问题