从单个数组作为列的多维数组(矩阵)

时间:2017-01-31 10:41:34

标签: excel vba excel-vba

我想知道如何在VBA中的动态多维数组中将单个矢量指定为specefic列/行。

我的意思是,如果我运行以下代码:     Dim m1()As Variant

ar1 = Array(1, 2)
ar2 = Array(4, 5)
ar3 = Array("a", "b")

m1 = Array(ar1, ar2, ar3)

我得到这样的东西:

(0 to 2), inside 3 arrays (0 to 1)

m1是Variant(0到2),在每个单元格中我都有一个维度为2 INSTEAD OF的数组:

m1作为变体(0到2,0到1),换句话说,一个矩阵,如果我将矩阵定义为变量(0到2,0到1),我会得到它并分别填充每个单元格。 像这样:

(1 to 2, 1 to 2)

我希望实现以下目标:

m3 = [ar1;ar2;ar3]

但是这次抛出

  

2015年错误

3 个答案:

答案 0 :(得分:0)

数字数组:具有数字索引的数组。值以线性方式存储和访问。

关联数组:一个以字符串作为索引的数组。这将元素值与键值相关联,而不是以严格的线性索引顺序存储。

多维数组:使用多个索引访问包含一个或多个数组和值的数组

数字数组    

  <?php
     /* First method to create array. */
     $numbers = array( 1, 2, 3, 4, 5);

     foreach( $numbers as $value ) {
        echo "Value is $value <br />";
     }

     /* Second method to create array. */
     $numbers[0] = "one";
     $numbers[1] = "two";
     $numbers[2] = "three";
     $numbers[3] = "four";
     $numbers[4] = "five";

     foreach( $numbers as $value ) {
        echo "Value is $value <br />";
     }
  ?>

这将产生以下结果 -

值为1

价值是2

价值是3

价值是4

价值是5

价值是一个

价值是两个

价值是三

价值是四

价值是五

关联数组

HTML&GT;    

  <?php
     /* First method to associate create array. */
     $salaries = array("jishan" => 2000, "bharat" => 1000, "abhi" => 500);

     echo "Salary of jishan is ". $salaries['jishan'] . "<br />";
     echo "Salary of bharat is ".  $salaries['bharat']. "<br />";
     echo "Salary of abhi is ".  $salaries['abhi']. "<br />";

     /* Second method to create array. */
     $salaries['jishan'] = "high";
     $salaries['bharat'] = "medium";
     $salaries['abhi'] = "low";

     echo "Salary of jishan is ". $salaries['jishan'] . "<br />";
     echo "Salary of bharat is ".  $salaries['bharat']. "<br />";
     echo "Salary of abhi is ".  $salaries['abhi']. "<br />";
  ?>

这将产生以下结果 -

吉山的薪水是2000

巴拉特的薪水是1000

abhi的薪水是500

吉山的薪水很高

巴拉特的薪水中等

abhi的薪水很低

多维数组

   

  <?php
     $marks = array( 
        "jishan" => array (
           "physics" => 35,
           "maths" => 30,   
           "chemistry" => 39
        ),

        "bharat" => array (
           "physics" => 30,
           "maths" => 32,
           "chemistry" => 29
        ),

        "abhi" => array (
           "physics" => 31,
           "maths" => 22,
           "chemistry" => 39
        )
     );

     /* Accessing multi-dimensional array values */
     echo "Marks for jishan in physics : " ;
     echo $marks['jishan']['physics'] . "<br />"; 

     echo "Marks for bharat in maths : ";
     echo $marks['bharat']['maths'] . "<br />"; 

     echo "Marks for abhi in chemistry : " ;
     echo $marks['abhi']['chemistry'] . "<br />"; 
  ?>

这将产生以下结果 -

物理学中的吉山标记:35

数学中的巴拉特标记:32

标记化学中的abhi:39

答案 1 :(得分:0)

如果我跟随,就像这样

Sub MatrixTest()

ar1 = Array(1, 2)
ar2 = Array(3, 4)
ar3 = Array("a", "b")

a = Array(ar1, ar2, ar3)
a = Application.Transpose(a)

End Sub

答案 2 :(得分:0)

如果您不介意从0到1移动,并且您的行数不超过65536&#39;,只需添加:

m1 = Application.Index(m1, 0, 0)