PDA

View Full Version : Solved: Vertically Merged Cells



DotNetGuy
08-28-2005, 09:39 AM
Good day;

I need to search the contents of the first cell of each row of all tables in my document for a specific string. I've found some good code here, like determining if a table cell is empty, however, I'm getting an exception when I run in to a table where there are vertically merged cells. Can anyone give me some guidance on how to handle the exception or better yet successfully search the vertically merged cells?

Many thanks.

p.s. - Several of the tables have nested tables but none of the child tables will ever have the text I'm looking for.



Dim tempTable As table
Dim tempRow As row
Dim tempCell As cell
Dim tempText As String
Dim i As Integer
Dim rowCount As Integer
Dim tableCount As Long
Dim cel As cell

' CountReqRows Macro
' Macro created 28 August 2005
'
' This macro will inspect ALL the tables in the document
' looking for the text "SA" in the first cell of each row

For Each tempTable In ActiveDocument.Tables
For Each tempRow In tempTable.Rows
For i = 1 To 1
If InStr(tempRow.Cells(1).Range.Text, "SA") > 0 Then
rowCount = rowCount + 1
End If
Next i
Next tempRow
Next tempTable
MsgBox ("Total target rows found are: " & rowCount )
rowCount = 0
End Sub

TonyJollans
08-28-2005, 10:25 AM
Hi DotNetGuy,

Welcome to VBAX!

Yes, the Rows and Columns collections can't always be used, but the Range.Cells collection is always accessible and each Item in it has a ColumnIndex property so if you run through checking that you will be able to find all the cells you want. I'm afraid I don't know a way of doing it without traversing all the cells in the table (first row would be easier because you could stop when you got to the second, but first column I think you just have to scan the lot).

DotNetGuy
08-28-2005, 11:09 AM
Tony;

Thanks for the heads-up about the ColumnIndex. That did the trick!




Sub CountReqRows()
Dim tempTable As Table
Dim cel As Cell
Dim tally As Integer

For Each tempTable In ActiveDocument.Tables
For Each cel In tempTable.Range.Cells
If cel.ColumnIndex = 1 Then
If InStr(cel.Range.Text, "SA") > 0 Then
tally = tally + 1
End If
End If
Next cel
Next tempTable
MsgBox ("Total found are: " & tally)
tally = 0
End Sub

TonyJollans
08-28-2005, 09:14 PM
Hi DotNetGuy,

It occurred to me after posting that if all you have is vertically merged cells and what you want is the first cell in each Row then, although you can't access the rows, you should be able to use .Columns(1), something like ..


For Each Cel in tempTable.Columns(1).Cells