0

我正在尝试使用 VBA 创建一个 varcov 矩阵,但是尽管尝试了数小时来寻找这个问题的答案,但仍然无法解决它。我的问题是,我在以下双星号行的每一行上都不断收到运行时错误“9”:

Sub varcovmmult()

    Dim returns()
    Dim trans()
    Dim Excess()
    Dim MMult()

    ReDim trans(ColCount, RowCount)
    ReDim Excess(RowCount, ColCount)
    ReDim MMult(ColCount, ColCount)
    ReDim returns(ColCount)

'Calculate mean, trans and excess arrays for dimensionalisation

'For mean:

    ColCount = Range("C6:H15").Columns.Count
    RowCount = Range("C6:H15").Rows.Count

    For j = 1 To ColCount
          **returns(j) = Application.Average(Range("C6:H15").Columns(j))
          Range("c30:h30").Cells(j) = returns(j)**
    Next j

'For excess:

    For j = 1 To ColCount
        For i = 1 To RowCount
            **Excess(i, j) = Range("c6:h15").Cells(i, j) - returns(j)
            Range("C36:H45").Cells(i, j) = Excess(i, j)**
        Next i
    Next j

'For tranpose:

    For j = 1 To ColCount
        For i = 1 To RowCount
            **trans(j, i) = Range("C36:H45").Cells(i, j)
            Range("C51:L56").Cells(j, i) = trans(j, i)**
        Next i
    Next j


'inject values into product array

    For i = 1 To ColCount
        For j = 1 To ColCount
            For k = 1 To RowCount
                **MMult(i, j) = MMult(i, j) + trans(i, k) * Excess(k, j)**
            Next k
        Next j
    Next i

'output product array values into varcov matrix and divide by n.years

    For i = 1 To ColCount
        For j = 1 To ColCount
            **Range("C62").Cells(i, j) = MMult(i, j)**
        Next j
    Next i

End Sub
4

2 回答 2

1

你需要把这些行:

ReDim trans(ColCount, RowCount)
ReDim Excess(RowCount, ColCount)
ReDim MMult(ColCount, ColCount)
ReDim returns(ColCount)

在这些行之后:

ColCount = Range("C6:H15").Columns.Count
RowCount = Range("C6:H15").Rows.Count
于 2013-03-31T07:27:44.903 回答
0

我正在尝试在 Range(C6:H15) 中的每个单元格中运行值为 1 的以下代码:

Sub varcovmmult()

    Dim returns()
    Dim trans()
    Dim Excess()
    Dim MMult()

    ColCount = Range("C6:H15").Columns.Count
    RowCount = Range("C6:H15").Rows.Count

    ReDim trans(ColCount, RowCount)
    ReDim Excess(RowCount, ColCount)
    ReDim MMult(ColCount, ColCount)
    ReDim returns(ColCount)

    For j = 1 To ColCount
        returns(j) = Application.Average(Range("C6:H15").Columns(j))
        Range("c30:h30").Cells(j) = returns(j)
    Next j

    For j = 1 To ColCount
        For i = 1 To RowCount
            Excess(i, j) = Range("c6:h15").Cells(i, j) - returns(j)
            Range("C36:H45").Cells(i, j) = Excess(i, j)
        Next i
    Next j

    For j = 1 To ColCount
        For i = 1 To RowCount
            trans(j, i) = Range("C36:H45").Cells(i, j)
            Range("C51:L56").Cells(j, i) = trans(j, i)
        Next i
    Next j

    For i = 1 To ColCount
        For j = 1 To ColCount
            For k = 1 To RowCount
                MMult(i, j) = MMult(i, j) + trans(i, k) * Excess(k, j)
            Next k
        Next j
    Next i

    For i = 1 To ColCount
        For j = 1 To ColCount
            Range("C62").Cells(i, j) = MMult(i, j)
        Next j
    Next i

End Sub

我能够成功运行此代码。

如果此范围内任何单元格中的值为空白或非数字,我得到的一个错误是类型不匹配。

如果下标超出范围,则可以尝试使用 ColCount - 1 或 RowCount - 1。只需检查 Cell(i, j) 中是否存在适当的值。

希望这可以帮助!

维韦克

于 2013-03-31T07:35:14.743 回答