在VBA中从2D数组中分配1D数组? [英] Assign 1D array from 2D array in VBA?

查看:223
本文介绍了在VBA中从2D数组中分配1D数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以也许它的星期一,也许我是愚蠢的..但我不能为我的生活找出一个从2D阵列中的一行获得1D阵列的好办法。 (也许这不是一个真正的2D数组?)



无论如何,我有一个数组,我定义如下: dim myArr(2, 4)作为变体



我填充了值1 - 15,所以它看起来像:

  1,2,3,4,5 
6,7,8,9,10
11,12,13,14,15

所以现在我想要的是这个数组的一行。我可以想到的唯一方法是这样做:

  dim temp()as variant 
ReDim temp(lbound (myArr,2)to ubound(myArr,2))

对于i = 0到0
对于j = LBound(myArr,2)到UBound(myArr,2)
temp(j)= myArr(i,j)
下一个
下一个

但是我不喜欢这样,因为如果数组很大,那么这样做的时间可能会更长。我想我应该可以做到:

  dim temp as variant 
temp = myArr(0)'since myArr (0)是一个有5个点的1D阵列吗?

但没有..我得到一个错误的数字错误。



我还挖掘了以前的事情,发现了这个问题:如何比较表格中的两个整行和tim thomas的答案显示了使用转置,并提及如果您比较列'只使用一次转置,但它不起作用..如果我这样做: dim myArr(2,0)as variant 转置工作,但不是我现在有这样的方式。



我在其他语言(如C ++和Python)中发现了这个问题的无数答案,但我并不熟悉,所以我无法解释他们。



有更好的方法吗?
或者我坚持这个双循环,我不喜欢?



谢谢!

解决方案

这是一个独立的例子,说明了切割二维数组的一种方式:

  Sub ArraySlicing()

Dim arr(1到5,1到5)
Dim slice
Dim x,y
Dim a As Application

'填充具有值的样本2-D数组...
对于y = 1至5
对于x = 1至5
arr(y,x) =R& y& :C& x
下一个x
下一个y
'...完成设置样本数组

设置a =应用程序'这只是为了缩短以下行

'示例1:获取第一个列
slice = a.Transpose(a.Index(arr,0,1))
Debug.Print Join(slice,, )'显示我们得到的

'示例2:获取第二个行(注意双转置)
slice = a.Transpose(a.Transpose(a.Index(arr, 0)))
Debug.Print加入(切片,,)'显示我们得到的

End Sub

Index()给你一个2-d数组 - (x,1)或(1,x) - Transpose()将它转换为1-d数组。 / p>

So maybe its monday, maybe I'm stupid.. But I can't for the life of me figure out a good way to get a 1D array from a one row in a 2D array. (maybe it's not a "real" 2D array?)

Anyways, I have an array that I defined like this: dim myArr(2,4) as variant

I filled it with values 1 - 15 so it looks like:

1,2,3,4,5
6,7,8,9,10
11,12,13,14,15

So now what I want is a single row of that array. the only way I can figure out is by doing this:

    dim temp() as variant
    ReDim temp(lbound(myArr,2) to ubound(myArr,2))

    For i = 0 To 0
        For j = LBound(myArr, 2) To UBound(myArr, 2)
            temp(j) = myArr(i, j)
        Next
    Next

But I don't like that because if the array got huge, then the time it took to do that might be considerably longer. I THOUGHT I should be able to do:

dim temp as variant
temp = myArr(0) 'since myArr(0) is a 1D array with 5 spots right?

but no.. I get a 'wrong number of dimentions' error.

I also dug back through previous things and found this question: How to compare two entire rows in a sheet and the answer by tim thomas shows the use of transpose, and mentions if you were comparing columns you'd use only transpose once, but it doesn't work.. if I made it like: dim myArr(2,0) as variant the transpose works, but not the way I have it now.

I found countless answers to this question in other languages like C++ and Python but I'm not familiar at all with either so I couldn't interpret them.

Is there a better way of doing this? Or am I stuck with this diouble loop that I don't like?

Thanks!

解决方案

Here's a self-contained illustration of one way to "slice" a 2-D array:

Sub ArraySlicing()

Dim arr(1 To 5, 1 To 5)
Dim slice
Dim x, y
Dim a As Application

    'Populate a sample 2-D array with values...
    For y = 1 To 5
    For x = 1 To 5
        arr(y, x) = "R" & y & ":C" & x
    Next x
    Next y
    '...done setting up sample array

    Set a = Application 'this is just to shorten the following lines

    'Example 1: get the first "column"
    slice = a.Transpose(a.Index(arr, 0, 1))
    Debug.Print Join(slice, ", ") 'display what we got 

    'Example 2: get second "row" (note double transpose)
    slice = a.Transpose(a.Transpose(a.Index(arr, 2, 0)))
    Debug.Print Join(slice, ", ") 'display what we got

End Sub

Index() gives you a 2-d array - (x,1) or (1,x) - Transpose() will convert that to a 1-d array.

这篇关于在VBA中从2D数组中分配1D数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆