Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / November 2007

Tip: Looking for answers? Try searching our database.

arrays

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Hemant_india - 23 Nov 2007 23:12 GMT
hi
i have several arrays defined with dim statement
e.g. dim a(),dim b(),dimc() and so on
is it posssble to redim these arrays using for...each statement?
thanks
Signature

hemu

Bob Phillips - 23 Nov 2007 23:28 GMT
For Each ... 'whatever
   Redim Preserve array_name(1 To new_upper_bound)
   'do something
Next

Signature

---
HTH

Bob

__________________________________________
UK Cambridge XL Users Conference 29-30 Nov
http://www.exceluserconference.com/UKEUC.html

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

> hi
> i have several arrays defined with dim statement
> e.g. dim a(),dim b(),dimc() and so on
> is it posssble to redim these arrays using for...each statement?
> thanks
Hemant_india - 23 Nov 2007 23:38 GMT
hi bob
actually i am thinking of considering all arrays as collection of objects
for each obj in collection...and so on
Signature

hemu

> For Each ... 'whatever
>     Redim Preserve array_name(1 To new_upper_bound)
[quoted text clipped - 6 lines]
> > is it posssble to redim these arrays using for...each statement?
> > thanks
Chip Pearson - 24 Nov 2007 15:13 GMT
I think the only way to do a For Each would be to store the arrays in a
Collection and loop through that.

Dim Coll As New Collection

Sub AAA()
   Set Coll = Nothing
   Dim V As Variant
   Dim Arr1()
   Dim Arr2()
   Dim Arr3()
   Coll.Add Arr1
   Coll.Add Arr2
   Coll.Add Arr3

   For Each V In Coll
       ReDim V(1 To 3)
   Next V
End Sub

Signature

Cordially,
Chip Pearson
Microsoft MVP  - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

> hi
> i have several arrays defined with dim statement
> e.g. dim a(),dim b(),dimc() and so on
> is it posssble to redim these arrays using for...each statement?
> thanks
Alan Beban - 24 Nov 2007 20:58 GMT
> I think the only way to do a For Each would be to store the arrays in a
> Collection and loop through that.
[quoted text clipped - 15 lines]
>    Next V
> End Sub

I get an "Object variable or With block variable not set" error message
where indicated.

Alan Beban
Rick Rothstein (MVP - VB) - 25 Nov 2007 09:37 GMT
You should really be using the Option Explicit statement in your code
modules... then you would have gotten a "Variable Not Defined" error with
the Set Coll statement highlighted and that might have led you to the fact
that you did not include the

Dim Coll As New Collection

statement where Chip showed it in his posted code. Try adding that statement
in the (General)(Declarations) section of the code window and see if that
solves your problem.

By the way, to make the VBA editor automatically include the Option Explicit
statement, click Tools/Options on the VBA editor menu bar, select the Editor
tab on the dialog box that appears and put a check mark next to "Require
Variable Declaration", then OK out of the dialog box.

Rick

>> I think the only way to do a For Each would be to store the arrays in a
>> Collection and loop through that.
[quoted text clipped - 20 lines]
>
> Alan Beban
Hemant_india - 25 Nov 2007 12:54 GMT
thanks Chip
thnk u very much
Signature

hemu

> You should really be using the Option Explicit statement in your code
> modules... then you would have gotten a "Variable Not Defined" error with
[quoted text clipped - 38 lines]
> >
> > Alan Beban
Alan Beban - 25 Nov 2007 15:35 GMT
> You should really be using the Option Explicit statement . . . Try adding that
> statement in the (General)(Declarations) section of the code window and
> see if that solves your problem. . . .
> Rick

Thanks; it highlights the underlying problem. I now get an error message
"Subscript out of range" where indicated when I check to see if the
redimming was successful. It seems that in the loop, when V is redimmed,
the corresponding item of the collection is unaffected.

Alan Beban

>>> Dim Coll As New Collection
>>>
[quoted text clipped - 11 lines]
>>>        ReDim V(1 To 3)
>>>    Next V
       Debug.print UBound(Arr1) '<-------Error message
>>> End Sub
Chip Pearson - 25 Nov 2007 17:09 GMT
It was a mistake (inadequate testing) on my part. You'd need to add the
Redim'd array back into the Collection are remove the original element.  All
said and done, it is likely all overkill simply to For Each on a set of
arrays.

Dim Coll As New Collection

Sub AAA()
   Set Coll = Nothing   ' clear out the collection
   Dim V As Variant
   Dim N As Long
   Dim Arr1() As Long  ' declare some dynamic arrays
   Dim Arr2() As Long
   Dim Arr3() As Long
   Coll.Add Arr1    ' add them to the collection
   Coll.Add Arr2
   Coll.Add Arr3
   For N = 1 To Coll.Count
       V = Coll(N)
       ReDim V(1 To 3) ' redim each array 1 to 3
       V(1) = N * 10    ' enter some easiliy identiful values
       V(2) = N * 100
       V(3) = N * 1000
       Coll.Add V, before:=N ' insert newly redim'd array back to
collection
       Coll.Remove N + 1    ' remove original array
   Next N

   ''''''''''''''''''''''''''''''''''
   ' Confirmation of correct results.
   ''''''''''''''''''''''''''''''''''
   For N = 1 To Coll.Count
       V = Coll(N)
       Debug.Print "================="
       Debug.Print "ARRAY: " & N, " ALLOCATED: " & IsArrayAllocated(V)
       Debug.Print "VALUES OF ELEMENTS:"
       Debug.Print V(1), V(2), V(3)
   Next N
End Sub

Function IsArrayAllocated(A As Variant) As Boolean
'''''''''''''''''''''''''''''''''''''''''''''''''''''
' Ensure that a dynamic array is actually allocated.
'''''''''''''''''''''''''''''''''''''''''''''''''''''
   On Error Resume Next
   IsArrayAllocated = Not IsError(LBound(A)) And LBound(A) <= UBound(A)
End Function

And don't get started on the old "Ranges As Arrays" bit. Ranges are not
arrays.

Signature

Cordially,
Chip Pearson
Microsoft MVP  - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)

"Alan Beban" <unavailable@no.com> wrote in message news:emc%
23Vj3LIHA.4880@TK2MSFTNGP03.phx.gbl...

>> You should really be using the Option Explicit statement . . . Try adding
>> that statement in the (General)(Declarations) section of the code window
[quoted text clipped - 24 lines]
>        Debug.print UBound(Arr1) '<-------Error message
>>>> End Sub
Alan Beban - 25 Nov 2007 19:09 GMT
> And don't get started on the old "Ranges As Arrays" bit. Ranges are not
> arrays.

No, no. You somehow got it in your mind that I disagree with you on
that; I don't! I mentioned before about how irritating it was to me
(many years ago)) that IsArray(iVar) returned True when iVar was not an
array, but a range. But because it does, it is often necessary to
provide code to confirm that one is dealing with a true array and not a
multi-cell range that IsArray treats as an array.

(You may be thinking of my erstwhile suggestion that Ranges are
Collections, which you find almost as attractive as the idea that ranges
are arrays :-))

Regards,
Alan
Hemant_india - 25 Nov 2007 12:49 GMT
allen
sorry for answering this late
alen(arr,1) returns number of elements  in the first dimension of array-arr
Signature

hemu

> > I think the only way to do a For Each would be to store the arrays in a
> > Collection and loop through that.
[quoted text clipped - 20 lines]
>
> Alan Beban
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.