Set up array to write to table?

E

Ed

I haven't worked much with arrays, so please forgive me if this is a basic
question. I want to collect a series of text elements to write into a table
all in one go, rather than write to the table one by one as collected. If I
only had a one column table, I would just say
For x = 1 to 10
Table.Cell(x, 1).Text = Array(x)
Next x

But when I've got four columns, how do I say
Table.Cell(x, y).Text = Array(x,y) ??
(I figure that's not the correct way to do it - but it was the closest I
could come to explaining it.)
Any help is appreciated.

Ed
 
E

Ed

It *is* that simple! I better go back and reread that section on arrays!
Thank you, Jonathan.

One more thing I think I _did_ understand - I've used ReDim Preserve with a
single dimension to add another value to the array while writing to it, but
you can't use Preserve with a multidimensional array, correct? So I would
need to declare this with more than the paces I will need, then ReDim (if I
want to) to match the actual number of values assigned?

Ed
 
G

Greg

Ed,

I am not a well versed array man, myself. While I haven't graduated to
columns, I was able to populate a 3x3 table using the following method.


Sub FillTable()
Dim i As Long, j As Long

Dim myArray(2, 2) As String
myArray(0, 0) = "A1"
myArray(0, 1) = "B1"
myArray(0, 2) = "C1"
myArray(1, 0) = "A2"
myArray(1, 1) = "B2"
myArray(1, 2) = "C2"
myArray(2, 0) = "A3"
myArray(2, 1) = "B3"
myArray(2, 2) = "C3"
For i = 0 To UBound(myArray)
For j = 0 To UBound(myArray)
ActiveDocument.Tables(1).Cell(i + 1, j + 1).Range.Text = myArray(i,
j)
Next j
Next i
End Sub

Note you add "1" to j and i in the cell designator to account for the
fact the first storage location in the array (if that is the rigth term
is a 0).

I will see if I can nuke out the four columns.
 
J

Jonathan West

Ed said:
It *is* that simple! I better go back and reread that section on arrays!
Thank you, Jonathan.

One more thing I think I _did_ understand - I've used ReDim Preserve with
a single dimension to add another value to the array while writing to it,
but you can't use Preserve with a multidimensional array, correct? So I
would need to declare this with more than the paces I will need, then
ReDim (if I want to) to match the actual number of values assigned?

You can use Preserve to modify the size of the last dimension
 
G

Greg

Ed,

I see Jonathan has come along with answers so this may be mute:

Sub FillTable2()
Dim i As Long, j As Long
Dim myArray(1, 3) As String
myArray(0, 0) = "A1"
myArray(0, 1) = "B1"
myArray(0, 2) = "C1"
myArray(0, 3) = "D1"
myArray(1, 0) = "A2"
myArray(1, 1) = "B2"
myArray(1, 2) = "CD"
myArray(1, 3) = "D3"
For i = 0 To UBound(myArray, 1)
For j = 0 To UBound(myArray, 2)
ActiveDocument.Tables(2).Cell(i + 1, j + 1).Range.Text = myArray(i,
j)
Next j
Next i
End Sub
 
G

Greg

Jonathan,

I hope Ed won't mind if I ask you a few questions in this thread.

If you noticed in my replies to Ed, I populated the array with
statements like:

myArray(0,0) = "A1"

In a single dimension array it seems much easier to populate like this:

Sub MakeAnArray1()
Dim myArray() As Variant
Dim i As Long
myArray = Array("One", "Two", "Three")
For i = 0 To UBound(myArray)
MsgBox myArray(i)
Next
End Sub

I can't figure out similiar way to populate a two dimenstional array.
Is the method above wrong or limited only to one dimensional arrays or
is there a faster way to populate a multi-dimensional array?

Second question,

With your For y = 1 to 4 and For x = 1 to 10 statements, how do you
load the data in the 0,0 0,1 and 1,0 locations in the array?

Thanks.
 
J

Jay Freedman

Hi Ed,

Sorry to pop your bubble, although it will save you some work...

You *can* use ReDim Preserve with a multidimensional array. It's just that
you're restricted to changing the number representing the *last* dimension.
For a two-dimensional array MyArray(x,y), you can change y but you can't
change x. In other words, you can make more rows but you can't change the
number of columns in the table. This is explained pretty well by the VBA
help topic on the ReDim statement.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.
 
E

Ed

Thanks for chiming in, Greg. The more students with questions, the more
answers come out!
Note you add "1" to j and i in the cell designator to account for the
fact the first storage location in the array (if that is the rigth term
is a 0).

Try this (copied from Help:Array):
You can use the Option Base statement at the top of a module to change the
default index of the first element from 0 to 1.

Option Base 1
Your first term is now "1" instead of "0".
Ed
 

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