vba 将VBA中多维数组的整行返回到一维数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14173622/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
return an entire row of a multidimensional array in VBA to a one dimensional array
提问by surya teja3
Is there any way to return the value of an entire row of a multidimensional array to a one dimensional array In VBA?
有没有办法将多维数组的整行的值返回到VBA中的一维数组?
Something like, arr_1dim = arr_2dim(3,:)
is a matlab expression for assigning the row 3 of arr_2dim
array to arr_1dim
in one single stretch.
类似的东西 arr_1dim = arr_2dim(3,:)
是一个 matlab 表达式,用于在一次拉伸中将arr_2dim
数组的第 3 行分配给arr_1dim
。
Is there any similar less expensive method in Excel VBA?
Excel VBA 中是否有类似的更便宜的方法?
采纳答案by KekuSemau
No there is no VBA-function to get a row or column. You can only write it yourself, or take a look here:
http://www.cpearson.com/excel/vbaarrays.htm
不,没有 VBA 函数来获取行或列。只能自己写,或者看这里:http:
//www.cpearson.com/excel/vbaarrays.htm
回答by rxex
There is a simple way to get a column or a row of a two-dimensional array. Assign a zero to column to get the row, or assign a zero to row to get the column, thus:
有一种简单的方法可以获取二维数组的一列或一行。为列分配零以获取行,或为行分配零以获取列,因此:
Application.WorksheetFunction.Index(array, 0, columnyouwant) /* or */
Application.WorksheetFunction.Index(array, rowyouwant, 0)
See here: How do I slice an array in Excel VBA?
请参阅此处: 如何在 Excel VBA 中对数组进行切片?
回答by Elias
This is what I do to easily print out one dimension of a multidimensional array.
这就是我为轻松打印出多维数组的一维所做的工作。
Basically, I define a new, 1D array and stuff it with the values from the bigger array.
基本上,我定义了一个新的一维数组,并用更大数组中的值填充它。
Example (3D to 1D to Printout):
示例(3D 到 1D 到打印输出):
Sub PrintItOut()
ReDim big_array(10,5,n) as Variant, small_array(n) as Variant
'use multidimensional array
'place multi-dimensional values into the 1D array
For i = 0 to n
small_array(i) = big_array(0, 0, i)
Next
Range(Cells(1, 1), Cells(1, n + 1)) = small_array
End Sub
That's how I do it. I hope that makes sense to whoever may be reading it. I think it's a very simple way to do it.
我就是这样做的。我希望这对阅读它的人有意义。我认为这是一种非常简单的方法。
回答by bonCodigo
Matlab is such an awesome application to work when it comes to matrices, arrays, vectors... ;) But Excel is not that bad, it too is matrix based.
当涉及到矩阵、数组、向量……时,Matlab 是一个非常棒的应用程序…… ;) 但是 Excel 并没有那么糟糕,它也是基于矩阵的。
So Assuming you do not want to loop through. You may simply ouput your multi-D array
into a worksheet using Transpose
function.
所以假设你不想循环。您可以简单地multi-D array
使用Transpose
函数将您的输出到工作表中。
Then pull a Row
to your desired range size into an array using Transpose
.
然后Row
使用Transpose
.
Dim vArr as Variant
'--output multi-D array into worksheet
Sheets(2).Range("E2").Resize(UBound(multiDArray) + 1, _
UBound(Application.Transpose(multiDArray))) = multiDArray
'--pull back the row you need: we double transpose here to get 1D. Coz single transpose
'-- results in 2D array..
vArr = WorksheetFunctions.Transpose( _
WorksheetFunctions.Transpose(Sheets(1).Range("A2:G2").Value))
To be absolutely dynamic, you may resize
your range A2:G2
with a dynamic row count using the multi-D array row upperbound
:)
要绝对的动态,你可能resize
你range A2:G2
用多d动态行数array row upperbound
:)
回答by crx
Since I had this question by myself recently, I want to share my code. I wrote a ready-to-use function where you can choose if you want a column or a row to be extracted:
由于我最近自己有这个问题,我想分享我的代码。我编写了一个随时可用的函数,您可以在其中选择是否要提取列或行:
'*** Modul 1, define function ***
Function getOneLine(array2D As Variant, lineIndex As Integer, choice As String) As Variant
' returning one column or row of a 2D array
' array2D: 2 dimensional Array
' lineIndex: the index of column or row, starting at 0
' choice: "c" for column or "r" for row
Dim i, n As Integer
Dim oneLine As Variant
If choice = "c" Then
n = UBound(array2D, 2)
ReDim oneLine(n)
For i = 0 To n
oneLine(i) = array2D(lineIndex, i)
Next
getOneLine = oneLine
End If
If choice = "r" Then
n = UBound(array2D, 1)
ReDim oneLine(n)
For i = 0 To n
oneLine(i) = array2D(i, lineIndex)
Next
getOneLine = oneLine
End If
End Function
'*** Modul 2, call function ***
Sub SomeProcess()
' Creating a 3x2 Matrix
' (In VBA-arrays the column is indexed before the rows
' starting at 0. So 3x2 looks like 1x2)
Dim SomeArray(1, 2) As Variant
SomeArray(0, 0) = 1
SomeArray(0, 1) = 2
SomeArray(0, 2) = 3
SomeArray(1, 0) = 4
SomeArray(1, 1) = 5
SomeArray(1, 2) = 6
Dim oneLine As Variant
oneLine = getOneLine(SomeArray, 1, "c")
Debug.Print oneLine(2)
' prints 6
End Sub