JenniferMurphy
Well-known Member
- Joined
- Jul 23, 2011
- Messages
- 2,676
- Office Version
- 365
- Platform
- Windows
The code below was working. Then I added the second parameter (pNumRevs). Now the statement tagged below gets an error.
It's called from this sheet:
VBA Code:
Function AmazonRtg(pRtgs As String, _
Optional pNumRevs As Double = 0, _
Optional pCoef As Double = 0, _
Optional pExp As Double = 0) As Variant
AmazonRtg = "" 'Return null if pRtgRev not valid format
Const DefCoef As Double = 2.20296467 'The coefficient of the power function for alpha = 0.05
Const DefExp As Double = -0.5 'The exponent of the power function
Dim coef As Double 'The coefficient to be used
Dim exp As Double 'The exponent to be used
Dim Rtg As Double 'The rating to be used
Dim NumRevs As Double 'The number of ratings to be used
Dim arrParms As Variant 'The result of the split of rtg/#revs
If pCoef = 0 Then coef = DefCoef Else coef = pCoef '<<< Error: Constant expression required
If pExp = 0 Then exp = DefExp Else exp = pExp
If InStr(pRtgs, "/") = 0 Then 'If rating and #reviews are separate,
Rtg = pRtgs 'Use the input parameters
NumRevs = pNumRevs
Else 'If they are combined, split them
arrParms = Split(pRtgRevs, "/") 'Split the rating and the #reviews
If UBound(arrParms) <> 1 Then Exit Function 'If not 2 values, return null
If IsNumeric(arrParms(0)) And _
IsNumeric(arrParms(1)) Then 'If both are numeric,
Rtg = arrParms(0)
NumRevs = arrParms(1)
End If 'Else, return null
End If
AmazonRtg = Rtg - coef * NumRevs ^ exp 'Return adjusted rating
End Function
It's called from this sheet:
Amazon Ratings.xlsx | |||||
---|---|---|---|---|---|
B | C | D | |||
4 | Rating | #Revs | Adj Rating | ||
5 | 5.0 | 100 | #VALUE! | ||
Demo |
Cell Formulas | ||
---|---|---|
Range | Formula | |
D5 | D5 | =AmazonRtg([@Rating],[@['#Revs]]) |