Arrays VBA将数组子集的元素相乘
我有一个包含5个整数的数组。例如:Arrays VBA将数组子集的元素相乘,arrays,vba,Arrays,Vba,我有一个包含5个整数的数组。例如: myArray = Array(3, 5, 2, 9, 3) 我想写一个循环,循环数组的元素,每次迭代都返回当前元素和它右边所有元素的乘积 在上述示例中: 3 * 5 * 2 * 9 * 3 5 * 2 * 9 * 3 2 * 9 * 3 9 * 3 3 我的非VBA思想大致如下: for i in myArray product(myArray[i:]) 但我不知道
myArray = Array(3, 5, 2, 9, 3)
我想写一个循环,循环数组的元素,每次迭代都返回当前元素和它右边所有元素的乘积
在上述示例中:
3 * 5 * 2 * 9 * 3
5 * 2 * 9 * 3
2 * 9 * 3
9 * 3
3
我的非VBA思想大致如下:
for i in myArray
product(myArray[i:])
但我不知道如何在VBA中编写它,特别是因为它不支持数组子集设置。欢迎任何帮助 您需要两个循环:
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
您需要两个循环:
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
如果产品顺序不重要,则可以在阵列上反向循环,并在每次迭代时输出运行中的产品。如果产品顺序不重要,则可以在阵列上反向循环,并在每次迭代时输出运行中的产品。