Home |
Search |
Today's Posts |
#1
Posted to microsoft.public.excel.programming
|
|||
|
|||
Array Question
Hi all,
I have a problem about looping arrays. Consider the following program: Sub Testing2() Dim X(1 To 8) As Long Dim i As Long For i = LBound(X) To UBound(X) X(i) = Rnd * 10000 Next X will contain the random number as expected. However if I use FOR EACH loop, Sub Testing2() Dim X(1 To 8) As Long Dim Element As Variant For Each Element in X Element = Rnd * 10000 Next Then the each array element still contains 0! Why is that? Please advise. Frederick Chow Hong Kong. |
#2
Posted to microsoft.public.excel.programming
|
|||
|
|||
Array Question
Sub Testing2()
Dim X(1 To 8) As Long Dim i As Long Dim Element As Variant For i = LBound(X) To UBound(X) X(i) = Rnd * 10000 Next For Each Element In X Debug.Print Element Next End Sub works, but I can't say why it doesn't work for assignment. I do know I have seen an MS KB article that advises against using For ... Each with an array. Maybe this is why. Generally, I find it is easier to use what works than to wonder why. -- Regards, Tom Ogilvy "Frederick Chow" wrote in message ... Hi all, I have a problem about looping arrays. Consider the following program: Sub Testing2() Dim X(1 To 8) As Long Dim i As Long For i = LBound(X) To UBound(X) X(i) = Rnd * 10000 Next X will contain the random number as expected. However if I use FOR EACH loop, Sub Testing2() Dim X(1 To 8) As Long Dim Element As Variant For Each Element in X Element = Rnd * 10000 Next Then the each array element still contains 0! Why is that? Please advise. Frederick Chow Hong Kong. |
#3
Posted to microsoft.public.excel.programming
|
|||
|
|||
Array Question
Hello,
Iterating through an array with a 'For Each-Next' loop is read only. Here's a .Net article mentioning the same thing, can't find a VB 6 article on the subject, but I imagine you're faced with something similar happening under the hood: http://msdn.microsoft.com/library/en...ingForEach.asp Either way, it's slower and not recommended: http://support.microsoft.com/kb/q129931/ Stick with approach #1. Regards, Nate Oliver |
#4
Posted to microsoft.public.excel.programming
|
|||
|
|||
Thanks very much for your response (No content)
"Frederick Chow" wrote in message ... Hi all, I have a problem about looping arrays. Consider the following program: Sub Testing2() Dim X(1 To 8) As Long Dim i As Long For i = LBound(X) To UBound(X) X(i) = Rnd * 10000 Next X will contain the random number as expected. However if I use FOR EACH loop, Sub Testing2() Dim X(1 To 8) As Long Dim Element As Variant For Each Element in X Element = Rnd * 10000 Next Then the each array element still contains 0! Why is that? Please advise. Frederick Chow Hong Kong. |
Reply |
Thread Tools | Search this Thread |
Display Modes | |
|
|
Similar Threads | ||||
Thread | Forum | |||
array question | Excel Worksheet Functions | |||
Array Question | Excel Worksheet Functions | |||
Array question | Excel Programming | |||
Array question | Excel Programming | |||
array question | Excel Programming |