Why is this UDF getting this error?

JenniferMurphy

Well-known Member
Joined
Jul 23, 2011
Messages
2,676
Office Version
  1. 365
Platform
  1. Windows
The code below was working. Then I added the second parameter (pNumRevs). Now the statement tagged below gets an error.

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
BCD
4Rating#RevsAdj Rating
55.0100#VALUE!
Demo
Cell Formulas
RangeFormula
D5D5=AmazonRtg([@Rating],[@['#Revs]])
 

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
I found the problem. When I added the new parameter, I changed the name of the first one from pRtgRevs to pRtgs. I filed to change that name in the Split statement. When I fixed that, it works. I do not understand why that should have caused the error of the earlier statement. (sigh)
 
Upvote 0
Solution

Forum statistics

Threads
1,223,264
Messages
6,171,081
Members
452,377
Latest member
bradfordsam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top