我的代码存在问题。
编写代码的目的是计算随机构建的不同资产组合的变量。 当我尝试计算varians时会出现问题,让我感到困扰的是代码在循环外工作。
这是我遇到问题的代码:
Sub kovariansmatrice()
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Dim Var As Long
Dim k, j As Integer
Dim Random As Double
Dim i As Integer
Dim Fifth As Double
Dim varians As Variant
For k = 1 To 1571 Step 10
Cells(2, 1573) = k
For j = 1 To 5000
For i = 1 To 1571
Randomize
Random = Rnd(1)
Worksheets("Kovarians").Cells(3, i).Value = Random
Next i
Fifth = WorksheetFunction.Large(Sheets("Kovarians").Range("A3:BHK3"), k)
Worksheets("Kovarians").Cells(5, 1).Value = Fifth
Application.MMult(Application.MMult(Sheets("Kovarians").Range("A2:BHK2"), Sheets("Kovarians").Range("A11:BHK1581")), Application.Transpose(Sheets("Kovarians").Range("A2:BHK2"))) = varians
Worksheets("Kovarians").Cells(5, 3).Value = varians
Calculate
Sheets("Sheet3").Cells(j, k) = Sheets("Kovarians").Cells(5, 3)
Next j
Next k
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
End Sub
以下是包含在上述代码中的子。这个子代码没有任何问题。
Sub Prove()
Dim Random As Double
Dim i As Integer
Dim Fifth As Double
Dim varians As Variant
For i = 1 To 1571
Randomize
Random = Rnd(1)
Worksheets("Sheet1").Cells(3, i).Value = Random
Next i
Fifth = WorksheetFunction.Large(Range("A3:BHK3"), 5)
Worksheets("Sheet1").Cells(5, 1).Value = Fifth
varians = Application.MMult(Application.MMult(Range("A2:BHK2"), Range("A11:BHK1581")), Application.Transpose(Range("A2:BHK2")))
Worksheets("Sheet1").Cells(6, 1).Value = varians
End Sub
提前,谢谢你的时间。
答案 0 :(得分:1)
由于指定行上的=
订单,您的错误正在发生。而不是
Application.MMult(Application.MMult(Sheets("Kovarians").Range("A2:BHK2"), Sheets("Kovarians").Range("A11:BHK1581")), Application.Transpose(Sheets("Kovarians").Range("A2:BHK2"))) = varians
代码应该是
varians = Application.MMult(Application.MMult(Sheets("Kovarians").Range("A2:BHK2"), Sheets("Kovarians").Range("A11:BHK1581")), Application.Transpose(Sheets("Kovarians").Range("A2:BHK2")))