7

Why doesn't this function work?

Type =funtest(2.1) in Excel and it'll give me #VALUE!.

Public Function funtest(a As Double) As Double

Dim z, j, i As Integer
Dim matrix(3, 3, 3) As Double

For z = 0 To 3 Step 1
For j = 0 To 3 Step 1
For i = 0 To 3 Step 1

matrix(z, j, i) = a

Next i, j, z

funtest = Application.WorksheetFunction.Sum(matrix)

End Function
4

3 回答 3

3

WorksheetFunction.Sum将使用范围或二维数组。它出错是因为您传递给它一个 3 维数组。

所以,这行得通

Public Function funtest(a As Double) As Double
    Dim z As Long, j As Long, i As Long
    Dim matrix() As Double

    ReDim matrix(0 To 3, 0 To 4)
    For j = LBound(matrix, 1) To UBound(matrix, 1)
    For i = LBound(matrix, 2) To UBound(matrix, 2)
        matrix(j, i) = a
    Next i, j

    funtest = Application.WorksheetFunction.Sum(matrix)
End Function

注意我稍微修改了您的声明,请参阅答案末尾的注释。

要对高维数组求和,您需要进行一些循环。

一种选择(可能适合也可能不适合您的总体要求)是声明您的数组略有不同,如所谓的Jagged Array.

Public Function funtest2(a As Double) As Double
    Dim z As Long, j As Long, i As Long
    Dim matrix() As Variant
    Dim InnerMatrix(0 To 4, 0 To 4) As Double

    ' Dimension Jagged Array
    ReDim matrix(0 To 4)
    For i = LBound(matrix, 1) To UBound(matrix, 1)
        matrix(i) = InnerMatrix
    Next

    'Load Data into matrix
    For z = LBound(matrix) To UBound(matrix)
    For j = LBound(matrix(z), 1) To UBound(matrix(z), 1)
    For i = LBound(matrix(z), 2) To UBound(matrix(z), 2)
        matrix(z)(j, i) = a
    Next i, j, z

    ' Sum matrix
    For z = LBound(matrix) To UBound(matrix)
        funtest2 = funtest2 + Application.WorksheetFunction.Sum(matrix(z))
    Next
End Function

这是一个二维数组。然后Sum依次将其应用于每个内部数组。这样,至少您只循环一个维度,而不是全部三个。

请注意DimInteger
您必须指定所有As Type的,否则变量默认为Variant
在您的代码中z,并且j将是Variants

此外,在 32 位操作系统上使用Integer而不是Long实际上会适得其反:Long's 会稍微快一些。

于 2012-09-12T06:15:26.593 回答
0

当您说“我正在尝试解决 (3,3,3) 矩阵的简单情况,其中每个元素都等于某个双精度值 a”时,我将按字面意思理解您。这将做到这一点:

Public Function funtest(a As Double) As Double
   funtest = 4*4*4*a
End Function
于 2012-09-12T04:43:14.163 回答
-1

首先,当您得到#VALUE!这意味着存在错误时,可能意味着使用无效的矩阵。

要回答您的问题,您的代码不起作用,因为您的语法不正确。以下函数从值创建一个矩阵。

Function FQ_matrix_create(StartValue As Double, Interval As Double,
nrow As Long, ncol As Long) As Double()
Dim M() As Double
' Creates matrix with sequential element values with given row and
' column sizes. Fills matrix row-wise with numbers.
' - set Interval = 0 for constant element values
' - error input arguments nrow and ncol are not positive integers

要对值求和,请使用:

Function FQ_matrix_element_sum(M() As Double, SumOption As
MatrixDirection) As Double()
' Returns the sum of elements of matrix M, either row or column wise
' - Rowwise sum returns a horizontal 1xNcol matrix
' - Columnwise sum returns a vertical 1 xNrow matrix
' - Element sum (all elements) returns a 1x1 matrix
' - error if M is not a matrix
' - error if SumOption is not 1 (nRowWiseSum) or 2 (nColWiseSum) or
3 (nElementSum)

为了帮助您了解 Excel VBA 中的矩阵,这里有一个很好的资源: http: //finaquant.com/download/matrixvectorvba

具体来说,请查看网站上的 PDF 下载。

于 2012-09-12T03:54:53.453 回答