Reputation: 367
I have an array containing 5 integers. For example:
myArray = Array(3, 5, 2, 9, 3)
I want to write a loop that loops through the elements of the array, and on each iteration returns the product of the current element and all elements to the right of it.
In the example above:
3 * 5 * 2 * 9 * 3
5 * 2 * 9 * 3
2 * 9 * 3
9 * 3
3
My non-VBA thinking was along the lines of:
for i in myArray
product(myArray[i:])
But I don't know how to write this in VBA, especially since it doesn't support array subsetting. Any help is welcome.
Upvotes: 0
Views: 232
Reputation: 152660
You need two loops:
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
Upvotes: 2