Dynamic Arrays

Q

QB

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
 
G

Gary''s Student

try ReDim before the loop:

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

Jacob Skaria

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
 
R

Rick Rothstein

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top