Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/vba/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vba 对数平均_Vba_Excel - Fatal编程技术网

Vba 对数平均

Vba 对数平均,vba,excel,Vba,Excel,我正在开发一个自定义函数,用于计算范围的对数平均值。这篇文章的答案是错误的,但这是一个起点。问题在于计算范围值的反日志(10^(0.1x))。这是我的第一篇帖子,请原谅我的错误 这是我的密码: Function logavg(rngValues As Range) As Double Dim lSumofValues As Double Dim lCountofValues As Double Dim lAntilog As Double Dim rngLoop

我正在开发一个自定义函数,用于计算范围的对数平均值。这篇文章的答案是错误的,但这是一个起点。问题在于计算范围值的反日志(10^(0.1x))。这是我的第一篇帖子,请原谅我的错误

这是我的密码:

Function logavg(rngValues As Range) As Double

    Dim lSumofValues As Double
    Dim lCountofValues As Double
    Dim lAntilog As Double
    Dim rngLoop As Range

    lSumofValues = 0
    lAntilog = 0
    lCountofValues = rngValues.Count 'Get count of values in items in range

'For loop - add the antilogs of the values in the range - does not work
    For Each rngLoop In rngValues
        lAntilog = WorksheetFunction.Power(10, 0.1 * rngLoop.Value)
        lSumofValues = lSumofValues + lAntilog
    Next

'Perform calculation - logarithmic average
    logavg = 10 * Log(lSumofValues / lCountofValues)

End Function
我尝试了这个“for”循环,但不起作用:

    For Each rngLoop In rngValues
        lSumofValues = lSumofValues + (10 ^ (0.1 * rngLoop.Value))
    Next
此代码用于简单(算术)平均值,因此我知道范围值正在正确传输和使用:

    For Each rngLoop In rngValues
    lSumofValues = lSumofValues + rngLoop.Value
    Next
    logavg = lSumofValues / lCountofValues
测试数据为:92.8,79.1,81.6,78.3,89.4,86.5,86.9

算术平均值为84.9,对数平均值为87.6

计算B2:B8对数平均值的两个Excel公式为:

a) 数组公式=10*LOG(和(10^(0.1*B2:B8))/COUNT(B2:B8)),和

b) 标准公式=10*LOG(SUMPRODUCT(10^(0.1*B2:B8))/计数(B2:B8))

谢谢。

小小的变化:

Function logavg(rngValues As Range) As Double
    With Application.WorksheetFunction
        Dim lSumofValues As Double
        Dim lCountofValues As Double
        Dim lAntilog As Double
        Dim rngLoop As Range

        lSumofValues = 0
        lAntilog = 0
        lCountofValues = rngValues.Count 'Get count of values in items in range

    'For loop - add the antilogs of the values in the range - does not work
        For Each rngLoop In rngValues
            lAntilog = .Power(10, 0.1 * rngLoop.Value)
            lSumofValues = lSumofValues + lAntilog
        Next

    'Perform calculation - logarithmic average
        logavg = 10 * .Log10(lSumofValues / lCountofValues)
    End With
End Function


如果我的数据在B1B7

以下是使用所有VBA函数的方法:

Function logAvg(myRng As Range) As Double
    Dim myCount As Long
    Dim D As Double
    Dim I As Long
    Dim V

    V = myRng
    For I = 1 To UBound(V, 1)
        D = D + 10 ^ (V(I, 1) * 0.1)
    Next I

    myCount = UBound(V, 1)
    D = D / myCount

    D = Log(D) / Log(10)
    logAvg = 10 * D

End Function

您是否只需要log base 10 average
logavg=10*WorksheetFunction.Log10(lSumofValues/lcontofValues)
而不是natural log?为什么不直接使用工作表公式呢?请注意,公式中的
log
是一个log base 10,而VBA中的
log
是一个log base e。要使用VBA获取以10为基数的对数:
log(x)/log(10)
@sjr Yes我需要以10为基数的对数平均值,而不是自然对数。工作表公式在一次更改几十个公式时效率太低。@FlorentB我不知道VBA日志是自然日志,所以谢谢。