- For individual users
- Instant access to solutions
- Ask your tech questions
- Start your 30-day Free Trial
Main Topics
Browse All Topicshi
I have a rather unusual question: I downloaded a function code of vba.
this function program is about monte calro sumiration of value at risk.
but actually i cant use thisfunction code.i understand only sub code. dose anyone have any idea how to use thisp program practically?
Any help would be most apreciated!
Many thanx and best regards
'Written by P.Urbani based on a spreadsheet by Carol Alexander.
'This function will produce a Monte Carlo Value at Risk simulation.
'The inputs are:
'VC - Variance Co Variance matrix
'PosnWeights - The actual posn values
'Iterations - Number of simulations to perform recomend (3000+)
'CL - The Confidence Level required typically 0.95,0.97 or 0.99
't - The holding period (same units as source data)
Function MC_Var(VC As Variant, PosnWeights As Variant, Iterations As Variant, CL As Variant, time As Variant) As Variant
Dim X, i, j As Variant
X = VC.Columns.Count
'STEP ONE READ IN VC
Dim matrix As Variant
ReDim matrix(X, X)
For k = 1 To X
For l = 1 To X
matrix(k, l) = VC(k, l)
Next l
Next k
'STEP TWO PERFROM CHOLESKY DECOMPOSITION ON VC MATRIX
Dim CMAT() As Variant
Dim a() As Variant
Dim M() As Variant
Dim y As Single
ReDim CMAT(X, X)
ReDim a(X, X)
ReDim M(X, X)
For i = 1 To X
For j = 1 To X
a(i, j) = matrix(i, j)
M(i, j) = 0
Next j
Next i
For i = 1 To X
For j = i To X
y = a(i, j)
For O = 1 To (i - 1)
y = y - M(i, O) * M(j, O)
Next O
If j = i Then
M(i, i) = Sqr(y)
Else
M(j, i) = y / M(i, i)
End If
Next j
Next i
CMAT = M
'STEP THREE TRANSPOSE CMAT
Dim Temparr3() As Variant
ReDim Temparr3(X, X)
Temparr3 = Application.WorksheetFunct
'STEP FOUR GENERATE (1 x X) VECTOR OF RANDOM STD NORMAL DEVIATES
'STEP FIVE MULTIPLY RND VECTOR BY TRANSPOSE OF CHOLESKY MATRIX
Dim RMAT() As Variant
Dim PLMAT() As Variant
Dim WVMAT() As Variant
Dim Temparr() As Variant
Dim Temparr6() As Variant
S = Iterations
ReDim RMAT(1, X)
ReDim WVMAT(1, X)
ReDim PLMAT(S, 1)
ReDim Temparr6(1, 1)
t = time
t = Sqr(t)
For p = 1 To S - 1 '(use this to loop around entire process)
For q = 1 To X
'Application.Volatile
e = Application.WorksheetFunct
RMAT(1, q) = e * t
Next q
'for normaly distributed use Application.WorksheetFunct
'for random letters use Chr(65 + Int(26 * Rnd))
'for std normal use Application.WorksheetFunct
'STEP SIX MULTIPLY RMAT BY TRANSPOSE OF CMAT TO GET WVMAT
TEMPARR4 = Application.WorksheetFunct
WVMAT = TEMPARR4
'STEP SEVEN MULITPLY WVMAT BY TRANSPOSE OF POSNWEIGHTS TO GET P&LMAT
Dim Temparr5() As Variant
ReDim Temparr5(1, X)
Temparr5 = Application.WorksheetFunct
Temparr6 = Application.WorksheetFunct
PLMAT(p, 1) = Temparr6(1)
Next p
'STEP EIGHT SORT P&L VALUES
'ReDim HOLD(1, 1) As Variant
'ReDim SRTD(S, 1) As Variant
'ReDim BTAR(f, 1) As Variant
'R = S
'For h = 1 To R - 1
'HOLD(h, 1) = PLMAT(h, 1)
'Next h
'sort
'For i = 1 To R - 1
' For j = i + 1 To R - 1
' If HOLD(j) >= HOLD(i) Then
' SRTD = HOLD(i) ' swap routine
' HOLD(i) = HOLD(j)
' HOLD(j) = SRTD
' 'Put below target values into new array
' If j < f Then
' BTAR(j) = HOLD
' End If
' End If
' Next j
'Next i
'TAKE 1-CL PERCENTILE = VaR
Dim final() As Variant
ReDim final(1, 1)
final(1, 1) = Application.WorksheetFunct
'AVERAGE OF VALUES BEYOND VaR = Conditional VaR
MC_Var = final 'PLMAT(S - 1, 1)
End Function
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Business Accounts
Answer for Membership
by: mdouganPosted on 2008-09-21 at 08:07:33ID: 22534922
The only difference between a Function and a Sub is that a Function has a return value, but a Sub does not.
So, when putting the function into your program, you'd just put the word Private or Public in front of it such as:
Public Function MC_Var(VC As Variant, PosnWeights As Variant, Iterations As Variant, CL As Variant, time As Variant) As Variant
Also, at the end of the Function, you would have an End Function statement instead of an End Sub statement, and if you had to exit the function, you would say Exit Function instead of Exit Sub.
And when calling the function you should provide a variable to receive the return value such as in the code below... note, the parameters sent to the function would be filled with appropriate values that the function is expecting to receive, just like a Sub.
Dim vReturn as Variant
vReturn = MC_Var(VC, PosnWeights , Iterations, CL , time )
Now, most of the syntax of this function is compatible with VB syntax. However, this function has several references to Application.Worksheet and then usually a reference to a particular formula or function. Those are Excel functions, or perhaps custom formulas that the author has embedded in the formula for a particular cell. You'd have to figure out how to code those referenced formulas or functions in VB, as VB doesn't necessarily have all of them.