当我尝试获得几百只股票的CoVar矩阵时,我试图使用变量来定义范围,其中我使用了以下代码。但是,系统会抛出RT错误1004。
Sub CoVar_Calc()
Dim i As Integer
Dim j As Integer
Dim ArrA As Range
Dim ArrB As Range
i = 1
j = 1
For i = 1 To 487
ArrA = Range(Sheets(2).Cells(7 + i, 2), Sheets(2).Cells(7 + i, 254))
For j = 1 To 487
ArrB = Range(Sheets(2).Cells(7 + j, 2), Sheets(2).Cells(7 + j, 254))
Sheets(3).Cells(j + 1, i + 1) = Application.WorksheetFunction. _
CoVar(ArrA, ArrB)
Next j
Next i
End Sub
当我第一次使用以下代码运行时
For i = 1 To 487
Set ArrA = Sheets(2).Range(Sheets(2).Cells(7 + i, 2), Sheets(2).Cells(7 + i, 254))
For j = 1 To 487
Set ArrB = Sheets(2).Range(Sheets(2).Cells(7 + j, 2), Sheets(2).Cells(7 + j, 254))
Sheets(3).Cells(j + 1, i + 1) = _
Application.WorksheetFunction. _
CoVar(ArrA, ArrB)
Next j
Next i
事情一切都很好,但第二次尝试运行它时,它给了我相同的RT 1004 ERROR消息。在两次运行中我没有对工作簿进行太多更改,但我确实打开了其他一些支持vba的工作簿。