加权平均 UDF
- 无论你在这里做什么,根据这个link,这不是你计算加权平均值的方式(最后一个参数似乎多余,在循环中划分是错误的)。
- 如果存在不包含数值的单元格,则提供的链接中建议的
SUMPRODUCT/SUM 公式解决方案将失败,而以下函数将忽略(跳过)这些单元格。
加权平均
Option Explicit
Function WeightedAverage( _
ByVal ScoreColumnRange As Range, _
ByVal WeightColumnRange As Range) _
As Double
' Compare the number of rows and use the smaller number.
Dim rCount As Long: rCount = ScoreColumnRange.Rows.Count
Dim wrCount As Long: wrCount = WeightColumnRange.Rows.Count
If wrCount < rCount Then rCount = wrCount
' Create the references to the column ranges.
Dim srg As Range: Set srg = ScoreColumnRange.Cells(1).Resize(rCount)
Dim wrg As Range: Set wrg = WeightColumnRange.Cells(1).Resize(rCount)
' Write the values from the column ranges to arrays.
Dim sData As Variant, wData As Variant
If rCount = 1 Then
ReDim sData(1 To 1, 1 To 1): sData(1, 1) = srg.Value
ReDim wData(1 To 1, 1 To 1): wData(1, 1) = wrg.Value
Else
sData = srg.Value
wData = wrg.Value
End If
' Declare additional variables to be used in the For...Next loop.
Dim sVal As Variant, wVal As Variant ' Current Values
Dim r As Long ' Rows Counter
Dim tWeight As Double ' Total Weight
Dim tProduct As Double ' Total Sum
' Calculate the total weights and the total products.
For r = 1 To UBound(sData, 1)
sVal = sData(r, 1)
If IsNumeric(sVal) Then ' prevent invalid score
wVal = wData(r, 1)
If IsNumeric(wVal) Then ' prevent invalid weight
tWeight = tWeight + wVal
tProduct = tProduct + sVal * wVal
End If
End If
Next r
If tWeight = 0 Then Exit Function ' all were invalid
' Calculate and return the weighted average (the result).
WeightedAverage = tProduct / tWeight ' maybe you want to round?
End Function
您的代码已修改
Function WeightedAverage( _
ByVal DataColumnRange As Range, _
ByVal ValuesColumnRange As Range, _
ByVal TotalValue As Double) _
As Double
' Compare the number of rows and use the smaller number.
Dim rCount As Long: rCount = DataColumnRange.Rows.Count
Dim vrCount As Long: vrCount = ValuesColumnRange.Rows.Count
If vrCount < rCount Then rCount = vrCount
' Create the references to the column ranges.
Dim drg As Range: Set drg = DataColumnRange.Cells(1).Resize(rCount)
Dim vrg As Range: Set vrg = ValuesColumnRange.Cells(1).Resize(rCount)
' Write the values of the column ranges to arrays.
Dim dData As Variant, vData As Variant
If rCount = 1 Then
ReDim dData(1 To 1, 1 To 1): dData(1, 1) = drg.Value
ReDim vData(1 To 1, 1 To 1): vData(1, 1) = vrg.Value
Else
dData = drg.Value
vData = vrg.Value
End If
' Declare additional variables to be used in the For...Next loop.
Dim dVal As Variant, vVal As Variant ' Current Values
Dim r As Long ' Rows Counter
Dim tCount As Long ' Total Count
Dim Total As Double ' Total Value
' Calculate the Total? (there should be a math term for it)
For r = 1 To UBound(dData, 1)
dVal = dData(r, 1)
If IsNumeric(dVal) Then ' prevent invalid data
vVal = vData(r, 1)
If IsNumeric(vVal) Then ' prevent invalid value
tCount = tCount + 1
Total = Total + dVal * vVal / TotalValue
End If
End If
Next r
If tCount = 0 Then Exit Function ' all were invalid
' Calculate and return the weighted average (the result).
WeightedAverage = Total / tCount ' maybe you want to round?
End Function