2017-08-27 297 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有什麼區別?這是否像內存分配? –

相關問題