Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to recognize merged cells in a table? help!!!!!

2,971 views
Skip to first unread message

Lek Ych

unread,
Jun 9, 2001, 3:52:19 AM6/9/01
to
Hi!
have to write again. I really need an answer.

i'm walking through a table, cell by cell, using VBA. and, for each cell, i
need to know whether it's
spanned over two or more rows( have it been merged with other cells). How
can i get this info?
In general, I wanna know a whole structure of a table, so my app could
recreate it.
would be glad to any hints concerning this theme.

Thanks in advance.
Lek Ych


Dave Rado

unread,
Jun 9, 2001, 8:25:19 AM6/9/01
to
Hi Lek

All I can think of is:

Selection.SelectRow
If Selection.Rows.Count > 1 Then
Msgbox "This cell spans more than one row"
End If

But I don't think that will really do what you want, at least not in complex
tables. I don't think you can do what you want in VBA :-(

Regards

Dave


"Lek Ych" <boo...@mail.ru> wrote in message
news:uIGJtjL8AHA.1988@tkmsftngp02...

Klaus Linke

unread,
Jun 9, 2001, 11:39:29 AM6/9/01
to
Hi Lek,

There have been some threads on that which you might locate by searching for
"rowspan" or "colspan" on www.googles.com (specify the microsoft.public.word.* newsgroups).

You can get the number of spanned rows, if you select the cell
(myCell.Select), then use
rowspan=Selection.Information(wdEndOfRangeRowNumber)-Selection.Information(wdStartOfRangeRowNumber).

But be careful: this does *not* work if you select the cell by hand or with
Selection.Expand Unit:=wdCell (probably a bug; I reported it as one to Microsoft.)

There doesn't seem to be a straightforward way to determine the number of
columns that a merged cell spans. A nice work-around was suggested by Jeff Hall:


> I use a work-around method for COLSPANs
>
> 1. calculate total width of table (in points)
> 2. get width of current cell in points
> 3. convert to a percentage of the width of the table (eg 27%)
> 4. use <TD COLSPAN=27 WIDTH=27%>
>
> This effectively breaks a table into 100 vertical columns, each 1% of the
> table width. The browser can easily display the cells accurately even when
> there are staggered or horizontally merged cells. No need for a load of
> complicated logic.
>
> Note that you should not use COLS=100 in the <TABLE> definition because
> (surpise surpise) NS does not like it!!

Greetings, Klaus Linke

Klaus Linke

unread,
Jun 9, 2001, 11:45:36 AM6/9/01
to

Dave Rado

unread,
Jun 9, 2001, 5:52:32 PM6/9/01
to
Hi Klaus

That rowspan trick doesn't work when I try it - it alwats seems to return 0
if a single cell is selected.

Regards

Dave


"Klaus Linke" <fotosatz...@t-online.de> wrote in message
news:eA3mhnP8AHA.1752@tkmsftngp03...

Klaus Linke

unread,
Jun 10, 2001, 6:44:10 AM6/10/01
to
Hi Dave,

I had an error in that -- you'd have to add 1 to the rowspan. The following
macro should be started in an empty document. I wrote it to demonstrate the
bug to the bug hunters at support.microsoft.com:

Sub TableBug()
'
' Selection.Information(wdEndOfRangeRowNumber) gives different results
' depending on how you selected the cell
' - by hand (mouse) or by Selection.Expand Unit:=wdCell
' - by Cell.Select
'
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=2, _
NumColumns:=2, DefaultTableBehavior:=wdWord9TableBehavior, _
AutoFitBehavior:=wdAutoFitFixed
Selection.MoveDown Unit:=wdLine, Count:=1, Extend:=wdExtend
Selection.Cells.Merge
Selection.Collapse
Selection.Expand (wdCell)
SI_SR = Str$(Selection.Information(wdStartOfRangeRowNumber))
SI_ER = Str$(Selection.Information(wdEndOfRangeRowNumber))
Prompt$ = "row" & SI_SR & " / "
Prompt$ = Prompt$ & "row" & SI_ER & "(!)"
MsgBox Prompt$, vbOKOnly + vbInformation, "Selection.Expand -- row start/end:"
Selection.Collapse
ActiveDocument.Tables(1).Cell(1, 1).Select
SI_SR = Str$(Selection.Information(wdStartOfRangeRowNumber))
SI_ER = Str$(Selection.Information(wdEndOfRangeRowNumber))
Prompt$ = "row" & SI_SR & " / "
Prompt$ = Prompt$ & "row" & SI_ER & "(!)"
MsgBox Prompt$, vbOKOnly + vbInformation, "Cell.Select -- row start/end:"
End Sub

I use Word 2000, and the macro says row1 to row1 is selected when selecting
the merged cells with Selection.Expand, and row1 to row2 is selected when
selecting it with Cell.Select. If you select it by hand (with the mouse), you
also get a rowspan of 1 for all (merged) cells.
I don't know about other versions of Word (97/2002).

Greetings, Klaus

Dave Rado

unread,
Jun 10, 2001, 1:02:16 PM6/10/01
to
Hi Klaus

You're right, and the bug is also present in Word 97 - and in both versions,
the bug is also evident if you try to use ranges (Set MyRange =
ActiveDocument.Tables(1).Cell(1, 1).Range will give you a rowspan of 1
whether or not the cells are merged).

I wonder what the difference is in practice between selecting with the mouse
and using ActiveDocument.Tables(1).Cell(1, 1).Select!

Regards

Dave


"Klaus Linke" <fotosatz...@t-online.de> wrote in message

news:u3eVFnZ8AHA.1916@tkmsftngp04...

Lek Ych

unread,
Jun 14, 2001, 2:48:53 AM6/14/01
to
Thank you, all!

Well, i'll have to use Selection object, though, through whole my program i
used Range object only.

Dave wrote:
> I wonder what the difference is in practice between selecting with the
mouse
> and using ActiveDocument.Tables(1).Cell(1, 1).Select!

I think that'll not surprise after noticing that Selection.Expand
Unit:=wdCell doesn't work properly (in this matter), and it lays under
manual selection.

By the way, while searching the solution I detected that Cell() method and
ColumnIndex property for a cell don't work "synchronously" for table with
notorious merged cells. Imagine: a table 2x2 where cells (1,1) and (2,1)
(first column) been merged, you call Set myCell = ...Cell(2,1), then
myCell.ColumnIndex will return 2. As if Cell() think this cell is (2,1) and
cell itself believe its coordinates are(2,2).

Gone finishing program.
Lek Ych.


subayan...@gmail.com

unread,
Jul 7, 2016, 7:28:07 AM7/7/16
to
Yes, it's there. Here is the logical explanation how you will detect vertically merged cells. Horizontally merged cell also can detect, but it will need some more coding.

But, unfortunately, not by calling any WORD VBA method or accessing any property of cell or table. It's by applying successive steps.

Step1:
-> Detect Row Count and Column Count of the table

Step2:
-> Declare an string array with same dimension as table( Dim A$()). Fill each cell of the array with "<m>" string.

Step3:
-> Start from cell(1)
-> Then move cell by cell(Cell.Next method)
-> Get each cells RowIndex and ColumnIndex.
-> Erase the content "<m>" from the array, referenced by this RowIndex and ColumnIndex(A(RowIndex,ColumnIndex)=""
-> Repeat this step until Cell.Next ends.

Step4:
-> Now check each cell of the Array(A$()). Those cells have the"<m>" string, are merged. Merged with immediate top cell.

I've not placed any function or code block. Assume it'll be more easy.

Thanks

grzegorz.v...@gmail.com

unread,
Sep 24, 2017, 5:08:57 PM9/24/17
to
Makes sense.
Let me try.

I have a bit dirty solution for accessing text in table that contains merged cells. MS Word and matching VBS are here:
http://www.burlingtonvisionandlab.com/Downloads/AccesTextInMergedCells/




jamie....@gmail.com

unread,
Oct 15, 2017, 12:29:27 PM10/15/17
to
I found this thread when looking for the same answer for PowerPoint and based on your idea, I write this to output the merged state of any cells in a table to the Immediate window:

' Written by Jamie Garroch of YOUpresent.co.uk
' Purpose : Displays the merged state of cells in a table
Public Sub DetectMergedCells()
Dim lRow As Long, lCol As Long
Dim arrMerged() As String
With ActiveWindow.Selection.ShapeRange(1).Table
ReDim arrMerged(.Rows.Count, .Columns.Count)
For lRow = 1 To .Rows.Count
For lCol = 1 To .Columns.Count
.Cell(lRow, lCol).Select
arrMerged(lRow, lCol) = IIf(MultipleCellsSelected, "[m]", "[ ]")
Next
Next
For lRow = 1 To .Rows.Count
For lCol = 1 To .Columns.Count
Debug.Print arrMerged(lRow, lCol);
Next
Debug.Print
Next
End With
End Sub

' Supporting function for DetectMergedCells
' Determines if currently selected cell is actually more than a single cell i.e. merged
Private Function MultipleCellsSelected() As Boolean
Dim lRow As Long, lCol As Long
Dim counter As Long
With ActiveWindow.Selection.ShapeRange(1).Table
For lRow = 1 To .Rows.Count
For lCol = 1 To .Columns.Count
If .Cell(lRow, lCol).Selected Then counter = counter + 1
Next
Next
End With
If counter > 1 Then MultipleCellsSelected = True
End Function
0 new messages