Hi
I am struggling to work out how to get the vbyesno statements from a msgbox to work for my user defined function below.
the statements are
MsgBox "You have selected to use " & N & " days" & " of data" _
& " and you have selected a price range of:" & PriceRange.Address(external:=True) & "," & " is this OK?", vbYesNo
If MsgResult = vbNo Then End
and
msgBox "Would you like to use the annualised volatility to calculate the sharpe ratio of the selected range?", vbYesNo
If MsgResult = vbNo Then Exit Function
However both the do not work as the function keeps running. Please how can i get them to work and end the procedure once no is clicked? coding below.
Thanks
Function StkVol(N, PriceRange)
'-----------------Declaring Variables---------------------------------'
Dim i, DlyRet() 'Daily return at time i
ReDim DlyRet(N - 1) 'Dynamic range dependent on user input N
Dim MsgResult As VbMsgBoxResult
'*****************Check Input Data *************************************'
MsgBox "You have selected to use " & N & " days" & " of data" _
& " and you have selected a price range of:" & PriceRange.Address(external:=True) & "," & " is this OK?", vbYesNo
If MsgResult = vbNo Then End
MsgBox "Please note that prices must be arranged from newest to oldest"
'*****************Calculation********************************************'
'Calculate daily returns
For i = 1 To N - 1 'Specifying price range dependent on user input N
DlyRet(i) = Log(PriceRange(i) / PriceRange(i + 1)) 'In VBA Log function returns the natural logarithm of a number
Next
'Calculate and annualize standard deviation
StkVol = Application.StDev(DlyRet) * Sqr(252) '252 average number of trading days in a year
MsgBox "The annualised volatility for your selected range is: " & StkVol & ""
MsgBox "Would you like to use the annualised volatility to calculate the sharpe ratio of the selected range?", vbYesNo
If MsgResult = vbNo Then Exit Function
Bookmarks