Excel vba - 连接两个数组

时间:2017-09-05 09:23:15

标签: excel vba

我需要将两个数组vd和vd1连接到vdu。

arguments[0]

首先,我得到并且“超出范围”错误。

其次,最后我将有18个阵列需要加入,所以我不知道这是否是加入它们的最佳选择。

2 个答案:

答案 0 :(得分:2)

尝试

    ReDim vdu(LBound(vd) To UBound(vd) + UBound(vd1), 1 To 1)

    For i = LBound(vdu) To UBound(vdu)
        If i <= UBound(vd) Then
            vdu(i, 1) = vd(i, 1)
        Else
            vdu(i, 1) = vd1(i - UBound(vd), 1)
        End If
    Next i

第二部分问题的更新 我将您的合并代码转换为函数

Public Function MergeArrays(arr1 As Variant, arr2 As Variant) As Variant
    Dim i As Long
    Dim arr As Variant
    ReDim arr(LBound(arr1, 1) To UBound(arr1, 1) + UBound(arr2, 1), 1 To 1)

    For i = LBound(arr, 1) To UBound(arr, 1)
        If i <= UBound(arr1, 1) Then
            arr(i, 1) = arr1(i, 1)
        Else
            arr(i, 1) = arr2(i - UBound(arr1, 1), 1)
        End If
    Next i
    MergeArrays = arr
End Function

然后每次将每个数组传递给它1个。

arr = MergeArrays(vd1, vd2)
arr = MergeArrays(arr, vd3)
arr = MergeArrays(arr, vdx)

您可以通过将数组存储在数组或字典中并循环遍历

来遍历此循环

其他选项

Public Function MergeArrays(ParamArray arrays() As Variant) As Variant
    Dim i As Long, j As Long, cnter As Long, UBoundArr As Long, OldUBoundArray As Long
    Dim arr() As Variant

    For j = LBound(arrays) To UBound(arrays)
        UBoundArr = UBoundArr + UBound(arrays(j), 1)
    Next j

    ReDim arr(1 To UBoundArr, 1 To 1)
    For j = LBound(arrays) To UBound(arrays)
        For i = LBound(arrays(j)) To UBound(arrays(j))
            arr(i + OldUBoundArray, 1) = arrays(j)(i, 1)
        Next i
        OldUBoundArray = OldUBoundArray + UBound(arrays(j), 1)
    Next j

    MergeArrays = arr
End Function

此方法使用ParamArray。如果您不确定那是什么,那么实际上您可以将未指定数量的参数传递给该函数。因此,使用此功能,您可以组合任意数量的阵列(相同形状和相同的基础,即x到x,1到1),它将组合它们。像

一样打电话
arr = MergeArrays(vd, vd1, vd2,....,vd18)

答案 1 :(得分:0)

getMasterArray将返回一个数组,该数组将多达60个不同的2d数组合并为一个。 getMasterArray还为您提供了返回0或基于数组的选项。

Combine Arrays Demo

Sub TestgetMasterArray()
    Dim data
    data = getMasterArray(False, Range("List1").Value, Range("List2").Value, Range("List3").Value, Range("List4").Value)
    Worksheets("Result").Range("A1").Resize(UBound(data), UBound(data, 2)).Value = data
End Sub

Function getMasterArray(Base0 As Boolean, ParamArray Arrays() As Variant)
    Dim result As Variant, v As Variant
    Dim Count As Long, Count2 As Long, lowBound As Integer, lOffset As Integer, x As Long, x1 As Long, y As Long

    For Each v In Arrays
        Count = Count + UBound(v) + IIf(LBound(v) = 0, 1, 0)
        y = UBound(v, 2) + IIf(LBound(v, 2) = 0, 1, 0)
        If y > Count2 Then Count2 = y
    Next

    lowBound = IIf(Base0, 0, 1)
    ReDim result(lowBound To Count, lowBound To Count2)

    For Each v In Arrays
        If LBound(v, 2) > LBound(result, 2) Then
            lOffset = -1
        ElseIf LBound(v, 2) < LBound(result, 2) Then
            lOffset = 1
        End If

        For x = LBound(v) To UBound(v)
            For y = LBound(v, 2) To UBound(v, 2)
                result(lowBound, y + lOffset) = v(x, y)
            Next
            lowBound = lowBound + 1
        Next
    Next
    getMasterArray = result
End Function

ockaroo.com

生成的示例数据