Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBA - MS Word: Count columns in each row of table 1

Status
Not open for further replies.

Fenrirshowl

Technical User
Apr 29, 2003
357
GB
Hi guys

I have inherited over 1000 word documents contain tables. The tables have varying numbers of rows and columns but follow the same general format.

I need to read in the information in each row that has, say six columns, but ignore rows where cells have been merged and the column count for that row is less than six. The data is read into a txt file for speed for import to Excel on completion as a delimited file.

If a table has less than 6 columns the table is ignored.

I was hoping to use a FOR or WHILE loop referencing individual cells via objTab.Cell(x,y).Range.Text but y occasionally exceeds the number of columns for a given row.

I thought objTab.Rows(x).Columns.Count may work, but it didn't.

Any ideas?

Thank you in advance

Fen
 
I thought objTab.Rows(x).Columns.Count may work, but it didn't
Use this instead:
objTab.Columns.Count

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I use that in an earlier part of the macro but it doesn't seem to help in this part of the code.

It would help if I described the table a little more:

The first 4 rows of the table have general information in, who created it, when, a reference number, etc and then row 5 has general titles which may cover 2 or more columns so some of the cells are merged i.e. "Dates". In row 6, the title in row 5 is broken down further i.e. under dates we have "From" and "To". Data is rows 7 and beyond.

If the table extends over a page, the titles are repeated on the next page, as is the information section of the who and the when.

I was hoping to look at the number of columns in each row of the table (effectively to detect merged cells) to either ignore the information or to read in the information. If I try to read in the information, the code can be looking to read cell(30,3), when in fact row 30 only has 2 columns, causing an error.

 
And this ?
objTab.Rows(x).Cells.Count

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
That's the chappy!

Now you say it, it seems the logical extension (as columns didn't work). I don't know why I didn't think of trying that, but I didn't.

Many many thanks - you've just saved me a lot of time.

Regards

Fen
 
I need to read in the information in each row that has, say six columns, but ignore rows where cells have been merged and the column count for that row is less than six.
Code:
Dim aTable As Table
Dim aRow As Row
For Each aTable In ActiveDocument.Tables
   For Each aRow In aTable.Rows
      If aRow.Cells.Count = 6 Then
    [COLOR=red]    ' do the stuff if there are 6 cells
        ' any row not having 6 will be ignored[/color red]
      End If
   Next

"A little piece of heaven
without that awkward dying part."

advertisment for Reese's Peanut Butter Cups (a chocolate/peanut butter confection)

Gerry
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top