假设我有一个2D数组,
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中有一种简单的方法吗?
答案 0 :(得分:4)
您可以使用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
答案 1 :(得分:0)
据我所知,你是cannot do this without looping。
下面是一个如何通过创建另一个数组并用循环填充它来实现它的示例:
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