[英]VBA Public Function "Compile Error: Expected: List separator or )"
我正在編寫我的第一個 function 並且我正在為加權平均公式創建一個 function。
當我將它用作子並定義值時,這工作正常,但是在立即 window 中測試 function 時出現此錯誤。
我嘗試過使用數據類型無濟於事。
任何幫助將非常感激。
Public Function WeightedAverage(data() As Range, values() As Range, TotalValue As Range) As Double
Dim i As Integer
Dim Sum As Variant
For i = 1 To UBound(data)
Sum = Sum + values(i, 1) / TotalValue * data(i, 1)
Next i
WA = Sum / UBound(data)
End Function
正如@Skin 提到的,您可以稍微修改您的代碼。 我刪除了參數部分中的括號。 此外,您應該在一個單元格中調用 function 並將每個參數分配給一個單元格/一系列單元格。
變量“WA”未在代碼中的任何位置定義。 這可能會導致您的錯誤。 要從用戶定義的 function (UDF) 獲取返回值,您應該將變量“sum”分配給函數名稱。
Public Function WeightedAverage( _
data As Range, _
values As Range, _
TotalValue As Range _
) As Double
Dim i As Integer
'You need containers for your parameters, which can be more then one cell
Dim arrData As Variant: arrData = data
Dim arrValues As Variant: arrValues = values
Dim Sum As Double
'Both arrays need the same size, optional, I thought that might help a little bit
If UBound(arrData, 1) <> UBound(arrValues) Then Exit Function
For i = 1 To UBound(arrData, 1)
Sum = Sum + arrValues(i, 1) / TotalValue * arrData(i, 1)
Next i
'Assign the variable "sum" to the functions name
WeightedAverage = Sum / UBound(arrData, 1)
End Function
SUMPRODUCT/SUM
公式解決方案將失敗,而以下 function 將忽略(跳過)這些單元格。加權平均
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.