【问题标题】:VBA - run time error 424: object requiredVBA - 运行时错误 424:需要对象
【发布时间】:2017-08-27 05:42:13
【问题描述】:

我收到运行时错误 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

【问题讨论】:

    标签: excel vba types


    【解决方案1】:

    代码存在一些问题。

    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 = Application.sqrt(sumOfSquareDiff / count)
    

    sdExcludesZero = Sqr(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
    

    【讨论】:

    • 非常感谢。现在很清楚了。对范围和变体的类型感到困惑
    • @PakHoCheung - 您只需更改函数getRangeByYear 的返回类型。请参阅答案中的编辑。
    • 使用set有什么区别?是不是类似于内存分配?
    • @PakHoCheung - Set 关键字用于创建新对象。详情请见this
    猜你喜欢
    • 2013-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多