0

这就是我想要做的:假设你有一个动态数组,其维度可以从0x610x6(这意味着我们可以有从 0 到 10 的任何行,但列总是 6)。我一直在拼命地尝试创建一个函数(然后将其绑定到一个宏),该函数将使用第一个数组作为参数,并将创建第二个数组作为输出,其元素将是第一个数组的返回值。例如,如果我们有 1x6 的简单情况,则输出数组的元素为 5,每种情况下都由公式 给出(x_i+1 - x_i)/x_i, i=1, 2, ..., 6。此外,该函数必须能够绕过输入数组中的任何缺失值并忽略相应的不存在的返回值。整个事情必须在 VBA 脚本中完成。

自从我疯狂地寻求帮助已经两天了,但问题是我对 VBA 编程一无所知(我通常使用其他语言,如 MATLAB 或 Mathematica)所以这对我来说非常困难。我发现的任何解决方案都无法整合并实现我的目标。任何帮助是极大的赞赏。

4

1 回答 1

0

因为您没有提供代码,所以我无法准确确定您想要做什么,但这里有一个传递数组并返回您应该能够推断的数组的示例。

编辑:只是为了好玩,将其更新为最多可用于 3 维数组。

Public Sub Test()
    'Defines testArray as Variant 0 to 10
    Dim testArray(0 To 1, 0 To 6) As Long
    Dim returnArray() As Long
    Dim i As Long
    Debug.Print UBound(testArray, 2)
    'Populates testArray with Longs
    For i = 0 To UBound(testArray, 1)
        For j = 0 To UBound(testArray, 2)
            testArray(i, j) = (i + j) * 2
        Next
    Next
    'Passes testArray and returns ParseArray
    returnArray = addOne(testArray)
  End Sub

Public Function addOne(arrValues() As Long) As Variant
    Dim arrCopy() As Long
    Dim dimensionNum As Long, ErrorCheck As Long
    On Error Resume Next
    For dimensionNum = 1 To 60000
        ErrorCheck = LBound(arrValues, dimensionNum)
        If Err.Number <> 0 Then
            dimensionNum = dimensionNum - 1
            Exit For
        End If
    Next
    Dim i As Long, j As Long, k As Long
    'Copies passed array to avoid updating passed array directly
    arrCopy = arrValues
    'Adds 1 to each element of the array.
    If dimensionNum = 1 Then
        For i = LBound(arrCopy) To UBound(arrCopy)
            arrCopy(i) = arrCopy(i) + 1
        Next
    ElseIf dimensionNum = 2 Then

        For i = LBound(arrCopy) To UBound(arrCopy)
            For j = LBound(arrCopy, 2) To UBound(arrCopy, 2)
                arrCopy(i, j) = arrCopy(i, j) + 1
            Next
        Next
    ElseIf dimensionNum = 3 Then
        For i = LBound(arrCopy) To UBound(arrCopy)
            For j = LBound(arrCopy, 2) To UBound(arrCopy, 2)
                For k = LBound(arrCopy, 3) To UBound(arrCopy, 3)
                    arrCopy(i, j, k) = arrCopy(i, j, k) + 1
                Next
            Next
        Next
    Else
        MsgBox "Add function only works for three dimensions or fewer arrays"
    End If
    addOne = arrCopy
End Function
于 2012-09-26T20:24:17.547 回答