PDA

View Full Version : A question on User defined function



arrun
02-06-2009, 01:33 AM
Hi all,

I am trying to write an UDF like below :


function calc(dat as double, type as characters, weight as double)

end function

Here the argument "type" must be of two types i.e either "c" or "p". If user put any other value then there must be some error. Also if type="c" then user needs to put a value against parameter "weight" otherwise there is no need to put a value against that argument and default value of "weight" would be 0.94.

Can anyone guide me how to create that?

Regards,

Bob Phillips
02-06-2009, 03:30 AM
Function calc(pDat As Double, pType As String, Optional pWeight)
Dim tmp As Double
If pType = "c" Then

If IsMissing(pWeight) Then

calc = CVErr(xlErrRef)
Exit Function
Else

tmp = pWeight
End If
ElseIf pType <> "p" Then

calc = CVErr(xlErrRef)
Exit Function
Else

tmp = 0.94
End If

calc = pDat * tmp

End Function