VBA乘以数组子集的元素

时间:2016-06-01 15:40:33

标签: arrays vba

我有一个包含5个整数的数组。例如:

        array (size=1)
          0 =>
            object(stdClass)[1]
              public 'flightnum' => string 'IV7050' (length=6)
              public 'deptime' => string '2016-06-01 16:47:00' (length=19)
              public 'arrtime' => string '17:01:39' (length=8)
              public 'gs' => string '397' (length=3)
              public 'alt' => string '26955' (length=5)
              public 'lat' => string '41.340347359635' (length=15)
              public 'lng' => string '-72.46185414378' (length=15)
              public 'phasedetail' => string 'Climbing' (length=8)
              public 'timeremaining' => string '07:45:00' (length=8)
              public 'online' => string 'No' (length=2)
              public 'depicao' => string 'KJFK' (length=4)
              public 'arricao' => string 'LFPO' (length=4)
              public 'heading' => string '68' (length=2)
              public 'distremain' => string '3080' (length=4)
              public 'aircraftname' => string 'B747-400' (length=8)
              public 'zone' =>
                array (size=1)
                  0 =>
                    object(stdClass)[2]
                      ...
              public 'code' => string 'IV' (length=2)
              public 'pilotid' => string 'IV0018' (length=6)
              public 'firstname' => string 'Mattia' (length=6)
              public 'lastname' => string 'test' (length=4)
              public 'deplat' => string '40.6398' (length=7)
              public 'deplng' => string '-73.7789' (length=8)
              public 'arrlat' => string '48.7253' (length=7)
              public 'arrlng' => string '2.35944' (length=7)
              public 'route_details' =>
                array (size=0)
                  empty
              public 'percomplete' => float 2.32
              public 'distremaining' => string '3080' (length=4)
              public 'pilotname' => string 'Mattia' (length=6)

我想编写一个循环遍历数组元素的循环,并在每次迭代时将当前元素和所有元素的乘积返回到它的 right

在上面的示例中:

myArray = Array(3, 5, 2, 9, 3)

我的非VBA思维方式如下:

3 * 5 * 2 * 9 * 3
    5 * 2 * 9 * 3
        2 * 9 * 3
            9 * 3
                3

但我不知道如何在VBA中编写它,特别是因为它不支持数组子集。欢迎任何帮助。

1 个答案:

答案 0 :(得分:2)

你需要两个循环:

Sub foo()
Dim myArray
myArray = Array(3, 5, 2, 9, 3)
Dim i&, j&
Dim output As Double

For i = LBound(myArray) To UBound(myArray)
    output = 1
    For j = i To UBound(myArray)
        output = output * myArray(j)
    Next j
    Debug.Print output
Next i
End Sub

enter image description here