Tables--Reading them Quickly

S

stephenc

I'm no expert but I think you'll find this is a much faster way to loop
through all cells in a table:

Set oCell = oTable.Range.Cells(1).Range
Do
oCell.Style = '... or whatever you want to do here
Set oCell = oCell.Next(wdCell)
Loop

Hope this helps.
S
 
A

asrisl

I have a pretty fast machine (everything else I do goes at 'lightning
speed'). Yet it takes almost 20 seconds for my computer to read the 300
row/2 column table. Am I doing something inefficient with this formula?

-------snip-------------

Set otable = Selection.Tables(1)
NumRows=otable.Rows.count ' this ranges from 300 to 350
redim Name1(NumRows)
redim Name2(NumRows)
With otable
For nRow = 1 To NumRows
Name1(nRow) = .Cell(nRow, 1)
Name2(nRow) = .Cell(nRow, 2)
Next
End With

------snip--------------

What improvements are possible? Are reads from tables just inherently slow?
Thanks.


-- Ed
 
D

Dave Lett

Hi Ed,

You should have a look at the artilce "Maximising the performance of Word
tables" at http://word.mvps.org/faqs/tblsfldsfms/FastTables.htm. The article
addresses your issue:

When cycling through table cells, never refer to a table cell by its
coordinates; as that is horrendously slow, because it forces Word to
calculate from scratch, for every single cell, where in the document the
cell in question actually is.



You're doing just that with .Cell (nRow,1) and .Cell(nRow, 2). I think
that's why it's taking so long for your routine to run.

HTH,

Dave
 
H

Helmut Weber

Hi Ed,

have a look at this one:

Sub test68869()
Dim c As Cell
Dim l As Long
l = Selection.Tables(1).Rows.Count
ReDim Name1(l) ' As Variant
ReDim Name2(l) ' As Variant
x = Timer
With Selection.Tables(1)
l = 0
.Columns(1).Select
For Each c In Selection.Cells
l = l + 1
Name1(l) = c.Range.Text
Next
.Columns(2).Select
l = 0
For Each c In Selection.Cells
l = l + 1
Name1(l) = c.Range.Text
Next
End With
MsgBox Timer - x
End Sub

Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word 2002, Windows 2000
 
K

Klaus Linke

Another one:

Dim oTable As Table
Dim varTable As Variant
Dim Start
Dim NumRows As Long, nRow As Long
Start = Timer
Set oTable = Selection.Tables(1)
NumRows = oTable.Rows.Count ' this ranges from 300 to 350
ReDim Name1(NumRows)
ReDim Name2(NumRows)
varTable = oTable.Range.Text
varTable = Split(varTable, Chr(13) & Chr(7))
For nRow = 0 To NumRows Step 3
Name1(nRow) = varTable(nRow)
Name2(nRow) = varTable(nRow + 1)
Next
MsgBox Timer - Start

Regards,
Klaus
 

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