Excel VBA - 将一维数组添加到多维数组,无循环 [英] Excel VBA - Add 1-dimensional array to multi-dimensional array without looping

查看:194
本文介绍了Excel VBA - 将一维数组添加到多维数组,无循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个有关创建一个矩阵的问题,从单独的数组中,我们必须循环:



从一个函数中我得到一个数组(Return_Calc ,rows = n-1)。输入(n-1,j-1)= Return_Calc(Nav_Range)
/ code>

目前,我正在这样做:

  Temp = Return_Calc(Nav_range)

对于i = 1至n - 1

输出(i - 1,j - 1)= Temp我 - 1)

下一个i

当前选项有效。我只是想知道是否有另一种可能性没有循环。感谢您的帮助!

解决方案

我不知道您是否会对该提案感到满意。它提供了创建阵列数组的可能性,在某些情况下,它将类似于多维数组。你可以考虑以这种方式解决你的问题。



以下是一个示例代码,如何创建,以及可以从最终数组中检索数据。

  Sub Array_Workaround()

Dim oneDimArrA,oneDimArrB
oneDimArrA = Array(1,2,3,4)
oneDimArrB = Array(A ,B,C,D)
Dim multiDimArr

'创建多维数组
multiDimArr = Array(oneDimArrA,oneDimArrB)

'获取元素 - 与标准语法不同
Debug.Print multiDimArr(0)(0)' - > 1
Debug.Print multiDimArr(0)(1)' - > 2
Debug.Print multiDimArr(1)(1)' - > B

End Sub

提出的解决方案有一个重要的好处 - 每个内部数组可以有不同的维度。


I have a question regarding "creating a matrix" out of single arrays wihtout having to loop through:

From a function I get back one array with data (Return_Calc, rows = n-1). I am looking for something like

    Output(n-1, j-1) = Return_Calc(Nav_Range)

At the moment, I am doing this:

    Temp = Return_Calc(Nav_range)

    For i = 1 To n - 1

        Output(i - 1, j - 1) = Temp(i - 1)

    Next i 

The current option works. I was just wondering if there is another possibility without looping. Thanks for your help!

解决方案

I'm not sure if you would be happy with that proposal. It presents possibility of creating Array-of-Arrays which, in some situation, would work similar to Multidimensional Array. You could consider solving your problems this way.

Here is a sample code how to create and which way you could retrieve data from final array.

Sub Array_Workaround()

    Dim oneDimArrA, oneDimArrB
        oneDimArrA = Array(1, 2, 3, 4)
        oneDimArrB = Array("A", "B", "C", "D")
    Dim multiDimArr

    'creating multidemmnsional array
    multiDimArr = Array(oneDimArrA, oneDimArrB)

    'get element- different to standard syntax
    Debug.Print multiDimArr(0)(0)   '--> 1
    Debug.Print multiDimArr(0)(1)   '--> 2
    Debug.Print multiDimArr(1)(1)   '--> B

End Sub

There is one important benefit of presented solution- each internal array can have different dimension.

这篇关于Excel VBA - 将一维数组添加到多维数组,无循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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