Hey Everybody
I have a problem with the MMult function in VBA. Although I tried everything (also read the old post concerning MMult in 2006), I can't make it work. Here's my code. Everything works except for the MMult function.
If I paste the values from arrz and CovarMat in a Spreadsheet and use the MMult there it works perfectly. I think the problem must be the CovarMat definition, but I can't figure out why.
PS: the additional "for" will be used later
Thanks for your help
Timi
I have a problem with the MMult function in VBA. Although I tried everything (also read the old post concerning MMult in 2006), I can't make it work. Here's my code. Everything works except for the MMult function.
If I paste the values from arrz and CovarMat in a Spreadsheet and use the MMult there it works perfectly. I think the problem must be the CovarMat definition, but I can't figure out why.
Code:
Sub Spread_Sim()
Application.ScreenUpdating = False
Dim Spreads As Double
Dim n_sim As Double
n_sim = Range("n_sim")
'Polar method for x ~ N(0,1)
Dim i As Integer
Dim q, u1, u2, p As Double
Dim arrz() As Double
ReDim Preserve arrz(n_sim)
Dim CovarMat() As Variant
ReDim Preserve CovarMat(12, 12)
CovarMat = Range("CovarMat")
'For j = 1 To n_sim
For i = 1 To 12 Step 2
Do Until q <= 1 And q > 0
u1 = Rnd() * 2 - 1
u2 = Rnd() * 2 - 1
q = u1 ^ 2 + u2 ^ 2
Loop
p = Sqr((-2 * Application.WorksheetFunction.Ln(q)) / q)
arrz(i) = u1 * p
If i = 13 Then
Else
arrz(i + 1) = u2 * p
End If
q = 2
Next i
Spreads = Application.WorksheetFunction.MMult(arrz, CovarMat)
Excel.Worksheets("Changes in economic environment").Range("A180:L180") = Spreads
'Next j
End Sub
PS: the additional "for" will be used later
Thanks for your help
Timi