Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 27,285
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 71
Default 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   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 75
Default 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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
array question Len Case Excel Worksheet Functions 3 December 17th 07 09:48 PM
Array Question Renee Excel Worksheet Functions 0 June 29th 05 07:10 PM
Array question Jim Simpson Excel Programming 3 September 22nd 04 04:40 AM
Array question Lee Excel Programming 1 December 11th 03 08:01 PM
array question john petty Excel Programming 1 August 29th 03 04:57 PM


All times are GMT +1. The time now is 05:52 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"