Set up, write to, read from multidimension array?

E

Ed

I need to run through a doc and grab some text items, write these
strings into an array, and then at the end read the array into table
cells. I did it with a one-dimension array. But for some reason my
brain's drawing a blank on how to work with a multidimensional array.

Let's say I'm going to grab five strings from each doc. I've got a
count of all the files I'm going to process (cntFiles) and I'll
iterate to that count. So I declare this like
Dim ary1(cntFiles,5) As String
???

Then I write to it with
For x = 1 to cntFiles
ary1(x,1) = String1
ary1(x,2) = String2
ary1(x,3) = String3
ary1(x,4) = String4
ary1(x,5) = String5
Next x
??

And read back using similar syntax??

Do I have this right? If not, corrections are most welcome!
Ed
 
G

Greg Maxey

Ed,

I am far from an expert on arrays, but here is an example of loading
the contents of a 5X5 table into an array and then reading it back:

Sub Scrathmacro()
Dim myArray(4, 4) As String
Dim oTbl As Word.Table
Dim i As Long
Dim j As Long
Set oTbl = ActiveDocument.Tables(1)
For i = 1 To 5
For j = 1 To 5
myArray(i - 1, j - 1) = oTbl.Cell(i, j).Range.Text
Next j
Next i
For i = 0 To 4
For j = 0 To 4
MsgBox myArray(i, j)
Next j
Next i
End Sub
 
R

Rob

Dim ary1(cntFiles,5) As String

You can't do that. You'll need to either dimension it bigger than you'll
need or redim it with the variable once it has a value. Like this.

Dim ary1() as String

ReDim ary1( cntFiles, 5)
 
E

Ed

Hi, Greg.

I had
Fcnt = fso.GetFolder(hereName).Files.Count
Dim ary1(Fcnt, 5) As String
and it popped up with "Fcnt" highlighted telling me I needed a
constant expression.

Any ideas?
Ed
 
J

Jonathan West

Ed said:
Hi, Greg.

I had
Fcnt = fso.GetFolder(hereName).Files.Count
Dim ary1(Fcnt, 5) As String
and it popped up with "Fcnt" highlighted telling me I needed a
constant expression.

Any ideas?


At the start of the routine, include this line

Dim ary1() As String

Then at the line that triggers the error, change Dim to ReDim
 

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