PC Review


Reply
Thread Tools Rate Thread

Changing values in an array using for each

 
 
=?Utf-8?B?QW5kcmV3IEhhbGwgTlo=?=
Guest
Posts: n/a
 
      18th Mar 2007
I have a 2D array, say it is 20 x 30 and I want to change any zero values to
say 10. I would have expected the following smaller example to work but it
only changes the value for 'v' rather than the value of the array members.

Sub test()
Dim ar1(1, 1) As Variant, v As Variant
ar1(0, 0) = 1
ar1(0, 1) = 1
ar1(1, 0) = 1
ar1(1, 1) = 1
For Each v In ar1
v = 2
Next
End Sub

I can do it with the following but I was surprised to find for each did not
seem to work as I would have expected

Sub test()
Dim ar1(1, 1) As Variant, v As Variant, i, j
ar1(0, 0) = 1
ar1(0, 1) = 1
ar1(1, 0) = 1
ar1(1, 1) = 1
For i = 0 To UBound(ar1, 1)
For j = 0 To UBound(ar1, 2)
ar1(i, j) = 2
Next
Next
End Sub


Andrew
 
Reply With Quote
 
 
 
 
=?Utf-8?B?QW5kcmV3IEhhbGwgTlo=?=
Guest
Posts: n/a
 
      18th Mar 2007

I should add that I know the examples do not change zero values to 10 but if
they changed them at all then I would have a method I can use.
 
Reply With Quote
 
Bob Phillips
Guest
Posts: n/a
 
      19th Mar 2007
An array is not a collection, so it doesn't work like a collection.


--
---
HTH

Bob

(there's no email, no snail mail, but somewhere should be gmail in my addy)



"Andrew Hall NZ" <(E-Mail Removed)> wrote in message
news:99E338DE-5460-4194-A1FB-(E-Mail Removed)...
>I have a 2D array, say it is 20 x 30 and I want to change any zero values
>to
> say 10. I would have expected the following smaller example to work but it
> only changes the value for 'v' rather than the value of the array members.
>
> Sub test()
> Dim ar1(1, 1) As Variant, v As Variant
> ar1(0, 0) = 1
> ar1(0, 1) = 1
> ar1(1, 0) = 1
> ar1(1, 1) = 1
> For Each v In ar1
> v = 2
> Next
> End Sub
>
> I can do it with the following but I was surprised to find for each did
> not
> seem to work as I would have expected
>
> Sub test()
> Dim ar1(1, 1) As Variant, v As Variant, i, j
> ar1(0, 0) = 1
> ar1(0, 1) = 1
> ar1(1, 0) = 1
> ar1(1, 1) = 1
> For i = 0 To UBound(ar1, 1)
> For j = 0 To UBound(ar1, 2)
> ar1(i, j) = 2
> Next
> Next
> End Sub
>
>
> Andrew



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Prevent cell/array references from changing when altering/moving thecell/array nme Microsoft Excel Misc 1 19th Sep 2008 01:53 PM
Changing values of an array of arrays =?Utf-8?B?Sm9lIER1bmZlZQ==?= Microsoft Excel Programming 3 13th Jun 2007 01:55 PM
Array: Counting multiple values within array Trilux_nogo Microsoft Excel Worksheet Functions 4 16th Apr 2007 03:12 AM
search an array for values contained in another array =?Utf-8?B?Q2hlZXItUGhpbC1seQ==?= Microsoft Excel Programming 0 12th Apr 2007 09:44 PM
Changing multiple array arguments simultaneously for a array formu =?Utf-8?B?QmluZw==?= Microsoft Excel Misc 2 14th Oct 2004 03:27 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:55 AM.