阵列中的平均值

时间:2016-03-09 17:07:34

标签: arrays vba average

我正在寻找一种在2D数组中优雅平均的方法。我的代码适用于一小部分元素,但我需要一些可以用于大量元素的东西。

以下是我对少量元素的代码。此代码将在同一行上平均3个元素,并将答案返回到结果数组。如果50个元素怎么能重写呢?你可以想象这种方法不适用于大量元素。

    For i = 1 To UBound(TMP)
                results(i, 1) = ((TMP(i, 13) + TMP(i, 14) + TMP(i, 15)) / 3)
    Next i

4 个答案:

答案 0 :(得分:0)

您可以将所需的值移动到1D数组中,然后使用application.Average()函数。例如:

Dim rwarr() As Double

For i = 1 To UBound(TMP, 1)
    ReDim rwarr(1 To UBound(TMP, 2)) As Double
    For j = 1 To UBound(TMP, 2)
        rwarr(j) = TMP(i, j)
    Next j
    results(i, 1) =  Application.Average(rwarr)
Next i

答案 1 :(得分:0)

这是一个可以取二维数组每行中多列平均值的函数。

Public Function AverageRowsIn2DArray(Arr As Variant, LBoundCol As Integer, UBoundCol As Integer) As Variant
    Dim iNumValues As Integer
    iNumValues = UBoundCol - LBoundCol + 1
    If iNumValues < 1 Then GoTo ErrHandler
    On Error GoTo ErrHandler
    Dim retArr As Variant
    Dim inArr As Variant
    If TypeName(Arr) = "Range" Then
        inArr = Arr.Value
    Else
        inArr = Arr
    End If

    ReDim retArr(1 To UBound(inArr), 1 To 1)

    Dim i As Integer, j As Integer
    Dim dSum As Double

    For i = 1 To UBound(inArr)
        dSum = 0
        For j = LBoundCol To UBoundCol
            dSum = dSum + inArr(i, j)
        Next j
        retArr(i, 1) = dSum / iNumValues
    Next i

    AverageRowsIn2DArray = retArr
    Exit Function

ErrHandler:
    AverageRowsIn2DArray = 0
End Function

在您的具体情况下,您需要致电

Dim results() as Double
results = AverageRowsIn2DArray(TMP, 13, 15)

答案 2 :(得分:0)

感谢功能斯科特。我稍微修改了它以将输入限制为单行。它工作得很好!!谢谢。这是修改后的函数:

Public Function AverageRowsIn2DArray(Arr As Variant, Nrow As Integer, LBoundCol As Integer, UBoundCol As Integer) As Variant
    Dim iNumValues As Integer
    iNumValues = UBoundCol - LBoundCol + 1
    If iNumValues < 1 Then GoTo ErrHandler
    On Error GoTo ErrHandler
    Dim retArr As Integer
    Dim inArr As Variant
    If TypeName(Arr) = "Range" Then
        inArr = Arr.Value
    Else
        inArr = Arr
    End If

    Dim i As Integer, j As Integer
    Dim dSum As Double


    For i = LBoundCol To UBoundCol
        dSum = dSum + inArr(Nrow, i)
    Next i
    retArr = dSum / iNumValues


    AverageRowsIn2DArray = retArr
    Erase inArr
    retArr = 0
    Exit Function

ErrHandler:
    AverageRowsIn2DArray = 0
End Function

现在我的电话是,

results(i, 4) = AverageRowsIn2DArray(TMP, i, 13, 15)

答案 3 :(得分:0)

假设您在第一个排名中的元素少于30K,请使用WorksheetFunction object INDEX function剥离一行以处理AVERAGE

Sub avgVals()
    Dim tmp As Variant, results As Variant, a As Long

    With Worksheets("Sheet2")
        With .Cells(1, 1).CurrentRegion
            With .Resize(.Rows.Count - 1, .Columns.Count).Offset(1, 0)
                tmp = .Cells.Value2
            End With

            ReDim results(LBound(tmp, 1) To UBound(tmp, 1), 1 To 1)
            For a = LBound(tmp, 1) To UBound(tmp, 1)
                results(a, 1) = Application.Average(Application.Index(tmp, a, 0))
            Next a

            With .Resize(.Rows.Count - 1, 1).Offset(1, .Columns.Count)
                .Cells.Resize(UBound(results, 1), UBound(results, 2)) = results
            End With

        End With


    End With
End Sub

使用Excel Application object来促进INDEX切割数组不是最快的方法,最终它有限制,但它可以提供一种方便的方法来实现小批量的AVERAGE结果。