Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / April 2005

Tip: Looking for answers? Try searching our database.

Tables--Reading them Quickly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
asrisl - 06 Apr 2005 12:55 GMT
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
stephenc - 06 Apr 2005 13:09 GMT
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

> 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
[quoted text clipped - 19 lines]
>
>                 -- Ed
Dave Lett - 06 Apr 2005 13:59 GMT
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

> 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
[quoted text clipped - 19 lines]
>
>                 -- Ed
Helmut Weber - 06 Apr 2005 14:05 GMT
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
asrisl - 07 Apr 2005 11:34 GMT
Perfect!!  Thanks.
                -- Ed
Klaus Linke - 09 Apr 2005 01:07 GMT
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

> 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
[quoted text clipped - 19 lines]
>
>                 -- Ed
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.