将此 Average/STDEV excel 公式转换为 VBA

问题描述 投票:0回答:0

我是VBA的初学者,我在学校经常使用excel,我通常需要在excel中输入以下公式来计算数据: “计算平均值” =ROUND(AVERAGE(AVERAGEIF(D:D, FILTER(D:D, ISNUMBER(D:D)), C:C)),3-(1+INT(LOG10(ABS(AVERAGE(AVERAGEIF(D:D,过滤器(D:D, ISNUMBER(D:D)),C:C))))))) “计算STDEV” =ROUND(STDEV.S(FILTER(C:C, ISNUMBER(D:D))),3-(1+INT(LOG10(ABS(STDEV.S(FILTER(C:C, ISNUMBER(D:D)) )))))) “计算CV” =(C8/C7)*100 “计算RE” =((C7-C16)/C16)*100

另一个同学告诉我,我可以用VBA把它们做成函数,所以我试着用互联网把它们转录成函数,因为我对编程知之甚少。附件是我想出的,对于大多数代码,我不断收到错误或 0。我真的不知道我做错了什么。感谢任何帮助,谢谢

Function CalculateMean(rngAvg As Range, rngFilter As Range) As Double
    Dim numericRange As Range
    Dim avgNumeric As Double
    Dim avgFiltered As Double
    Dim roundDigits As Integer
    Dim roundedAvg As Double
    Dim cell As Range

    avgNumeric = 0
    avgFiltered = 0
    roundDigits = 0

    For Each cell In rngFilter
        If IsNumeric(cell.Value) Then
            avgNumeric = avgNumeric + cell.Value
            avgFiltered = avgFiltered + rngAvg.Cells(cell.row, 1).Value
        End If
    Next cell

    If avgNumeric > 0 Then
        avgNumeric = avgNumeric / numericRange.Cells.count
        roundDigits = 3 - (1 + Int(Log(Abs(avgNumeric)) / Log(10)))
        roundedAvg = WorksheetFunction.Round(avgFiltered / numericRange.Cells.count, roundDigits)
    Else
        roundedAvg = 0
    End If

    CalculateMean = roundedAvg
End Function
Function CalculateSTDEV(ByVal dataRange As Range) As Double
    Dim stdev As Double
    Dim count As Long
    Dim sum As Double
    Dim mean As Double
    Dim variance As Double
    Dim filteredData As Variant
    Dim i As Long
    
    'filter the data based on whether corresponding cells in column D contain numbers
    filteredData = Application.WorksheetFunction.filter(dataRange.Columns(1), IsNumber(dataRange.Columns(2)))
    
    'calculate the standard deviation of the filtered data
    stdev = Application.WorksheetFunction.StDev_S(filteredData)
    
    'calculate the number of data points in the filtered data
    count = Application.WorksheetFunction.count(filteredData)
    
    'calculate the sum and mean of the filtered data
    For i = 1 To count
        sum = sum + filteredData(i, 1)
    Next i
    mean = sum / count
    
    'calculate the variance of the filtered data
    For i = 1 To count
        variance = variance + (filteredData(i, 1) - mean) ^ 2
    Next i
    variance = variance / (count - 1)
    
    'calculate the number of decimal places to round the result to
    Dim decimalPlaces As Integer
    decimalPlaces = 3 - (1 + Int(Log10(Abs(stdev))))
    
    'round the standard deviation to the desired number of decimal places
    CalculateSTDEV = Round(stdev, decimalPlaces)
End Function

Function CalculateCV(cell1 As Range, cell2 As Range) As Double
' CV will be equal to SD divided by the Mean times 100

    MyFormula = (cell2.Value / cell1.Value) * 100
'
End Function

Function CalculateRE(inputRange As Range) As Double
    Dim numerator As Double
    Dim denominator As Double
    
' RE = ((mean - theoretical)/theoretical)*100

    numerator = inputRange(1, 1).Value - inputRange(10, 1).Value
    denominator = inputRange(10, 1).Value
    
    CalculatePercentage = (numerator / denominator) * 100
End Function


对于 CalculateMean,我尝试让函数对第二列进行排序,看看那里是否有数字。然后将计算它旁边数字的平均值(您可以尝试在 C 列 (15, 15.2, 15.1) 中键入以下内容,然后在 D 列 (0.5, 0.3) 中键入以下内容。您可以对 STDEV 使用相同的数据集.excel 公式完全有效,我只想将它集成到 excel 中,这样我就不需要去我的旧工作簿复制它了

excel vba
© www.soinside.com 2019 - 2024. All rights reserved.