Please let me know the error in this code

V

vbaseeker

Hello all,
I am beginner in VBa programming.
Please let me know the error in this code with syntax error in
function line.
Thanks in advance!!!

Sub Montecarlo()

Function callPriceMC(s0 As Double,k As Double, T As Double,r As
Double, q As Double, vol As Double,
numTrials As Long, seed As Long) As Double

Rnd (-20)
Randomize (seed)

Dim i As Long
Dim g As Double
Dim st As Double

callPriceMC = 0

For i = 1 To numTrials
g = Application.WorksheetFunction.NormSInv(Rnd())
st = s0 * Exp((r - q - vol * vol / 2) * T + g * vol * Sqr(T))
callPriceMC = callPriceMC + Application.WorksheetFunction.Max(st - k,
0)
Next i

callPriceMC = callPriceMC * Exp(-r * T) / numTrials
End Function

End Sub
 
J

joeu2004

Please let me know the error in this code with syntax
error in function line. [....]
Sub Montecarlo()

Function callPriceMC(s0 As Double,k As Double, T As Double,r As
Double, q As Double, vol As Double,
numTrials As Long, seed As Long) As Double [....]
 End Function

End Sub

You cannot nest procedures -- you cannot put a function inside a sub.

Simply remove the lines Sub Montecarlo() and End Sub.

But I did not bother read, much less test, the function itself to see
if there are other errors. I simply confirmed that removing the sub-
related lines eliminates all syntax errors, after I correct the lines
folded in the posting without continuation characters.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top