PC Review


Reply
Thread Tools Rate Thread

Dynamic Arrays

 
 
QB
Guest
Posts: n/a
 
      2nd Oct 2009
I am trying to generate an array on the fly

For x = 2 To flstRow
aCom(1, x - 2) = Range("A" & x).Value
aCom(2, x - 2) = Range("M" & x).Value
Next x

but when I run the code I keep getting a Subscript out of range error and it
highlights the x-2 part of my code. How can I loop through the row of a
table and populate a 2 dimensional array with the values?

QB
 
Reply With Quote
 
 
 
 
Gary''s Student
Guest
Posts: n/a
 
      2nd Oct 2009
try ReDim before the loop:

Sub sistence()
x = 100
ReDim aCom(2, x - 2)
End Sub

--
Gary''s Student - gsnu200906


"QB" wrote:

> I am trying to generate an array on the fly
>
> For x = 2 To flstRow
> aCom(1, x - 2) = Range("A" & x).Value
> aCom(2, x - 2) = Range("M" & x).Value
> Next x
>
> but when I run the code I keep getting a Subscript out of range error and it
> highlights the x-2 part of my code. How can I loop through the row of a
> table and populate a 2 dimensional array with the values?
>
> QB

 
Reply With Quote
 
Jacob Skaria
Guest
Posts: n/a
 
      2nd Oct 2009
What Gary meant is just above the loop...ReDim aCom(2, flstRow - 2)


Sub Macro()
Dim aCom As Variant, x As Long, flstRow As Long

flstRow = something
ReDim aCom(2, flstRow - 2)
For x = 2 To flstRow
aCom(1, x - 2) = Range("A" & x).Value
aCom(2, x - 2) = Range("M" & x).Value
Next x
End Sub
If this post helps click Yes
---------------
Jacob Skaria


"Gary''s Student" wrote:

> try ReDim before the loop:
>
> Sub sistence()
> x = 100
> ReDim aCom(2, x - 2)
> End Sub
>
> --
> Gary''s Student - gsnu200906
>
>
> "QB" wrote:
>
> > I am trying to generate an array on the fly
> >
> > For x = 2 To flstRow
> > aCom(1, x - 2) = Range("A" & x).Value
> > aCom(2, x - 2) = Range("M" & x).Value
> > Next x
> >
> > but when I run the code I keep getting a Subscript out of range error and it
> > highlights the x-2 part of my code. How can I loop through the row of a
> > table and populate a 2 dimensional array with the values?
> >
> > QB

 
Reply With Quote
 
Rick Rothstein
Guest
Posts: n/a
 
      2nd Oct 2009
I would probably do the ReDim statement this way...

ReDim aCom(1 To 2, 0 To flstRow - 2)

I know the "0 To" part of the second element is not required, but I think it
adds clarity... the "1 To" part of the first element insures an unused 0
index is not created for the first element.

--
Rick (MVP - Excel)


"Jacob Skaria" <(E-Mail Removed)> wrote in message
news:61AE5768-C9D9-48FA-AA86-(E-Mail Removed)...
> What Gary meant is just above the loop...ReDim aCom(2, flstRow - 2)
>
>
> Sub Macro()
> Dim aCom As Variant, x As Long, flstRow As Long
>
> flstRow = something
> ReDim aCom(2, flstRow - 2)
> For x = 2 To flstRow
> aCom(1, x - 2) = Range("A" & x).Value
> aCom(2, x - 2) = Range("M" & x).Value
> Next x
> End Sub
> If this post helps click Yes
> ---------------
> Jacob Skaria
>
>
> "Gary''s Student" wrote:
>
>> try ReDim before the loop:
>>
>> Sub sistence()
>> x = 100
>> ReDim aCom(2, x - 2)
>> End Sub
>>
>> --
>> Gary''s Student - gsnu200906
>>
>>
>> "QB" wrote:
>>
>> > I am trying to generate an array on the fly
>> >
>> > For x = 2 To flstRow
>> > aCom(1, x - 2) = Range("A" & x).Value
>> > aCom(2, x - 2) = Range("M" & x).Value
>> > Next x
>> >
>> > but when I run the code I keep getting a Subscript out of range error
>> > and it
>> > highlights the x-2 part of my code. How can I loop through the row of
>> > a
>> > table and populate a 2 dimensional array with the values?
>> >
>> > QB


 
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
dynamic arrays =?Utf-8?B?QlJIYW5jb2Nr?= Microsoft Access Form Coding 4 11th Aug 2006 03:30 PM
Dynamic Arrays in ASP.NET? tjonsek@phenom-biz.com Microsoft Dot NET 7 12th Dec 2005 07:15 PM
Dynamic Arrays in C++\CLI Herby Microsoft VC .NET 6 2nd Dec 2005 04:16 AM
Dynamic Arrays Alan Beban Microsoft Excel Programming 12 9th Dec 2004 12:50 AM
dynamic arrays Hugh Welford Microsoft Access 7 18th Jun 2004 05:47 PM


Features
 

Advertising
 

Newsgroups
 


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