1

假设我有一个二维数组,

dim iArray(1 to 2, 1 to 2) as integer
iArray(1,1)=1: iArray(1,2)=2: iArray(2,1)=3: iArray(2,2)=4
'iArray =   1  2
'           3  4

我想削减其中一个维度。
在 Matlab 中,您可以:

%Matlab style:
cutArray = iArray(:,2)
%cutArray =  2
%            4

在 VBA 中是否有一种简单的方法可以做到这一点?

4

2 回答 2

4

您可以使用 2D 数组访问行或列Application.Index(array, RowNum, ColNum)

展示

Sub Demo()
    Dim iArray(1 To 2, 1 To 2) As Integer
    iArray(1, 1) = 1: iArray(1, 2) = 2: iArray(2, 1) = 3: iArray(2, 2) = 4

    Dim aRow As Variant
    Dim aCol As Variant
    With Application
        ' Get Row 2
        aRow = .Index(iArray, 2)

        ' Get Column 2
        aCol = .Transpose(.Index(iArray, , 2))

    End With
End Sub
于 2013-11-11T05:53:37.553 回答
0

据我所知,如果没有循环,您将无法做到这一点

下面是一个示例,说明如何通过创建另一个数组并用循环填充它来做到这一点:

Sub arraySlice()
    Dim i As Integer
    Dim a1(1 To 2, 1 To 2) As Integer

    a1(1, 1) = 1
    a1(1, 2) = 2
    a1(2, 1) = 3
    a1(2, 2) = 4

    Dim a2(1 To 2) As Integer

    For i = 1 To UBound(a1)
        a2(i) = a1(i, 2)
    Next i

End Sub
于 2013-11-11T03:30:57.770 回答