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 / Tables / October 2006

Tip: Looking for answers? Try searching our database.

The tables are turned! Well... not yet.

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Cooz - 02 Jun 2006 15:49 GMT
Hi everyone,

Is there a way to turn a Word table so that its columns become rows and its
rows become columns? If yes... how?

Thank you,
Cooz
macropod - 02 Jun 2006 16:14 GMT
hi Cooz,

You can't turn rows into columns and vice-versa, as such. However, if all
you want to achieve is to have the text on side, select the relevant cells
and use Format|Text direction. Alternatively, put your table on a page laid
out in landscape if the rest of your document is portrait, or vice-versa.

Cheers

Signature

macropod
[MVP - Microsoft Word]

> Hi everyone,
>
[quoted text clipped - 3 lines]
> Thank you,
> Cooz
Suzanne S. Barnhill - 02 Jun 2006 16:53 GMT
Although "macropod" has given you the options within Word, there is a way to
transpose the rows and columns, but it will work only if the table is fairly
simple (no merged cells, not too much fancy formatting). Copy the table in
Word, then open Excel and use Paste Special: Transpose. This will swap the
rows and columns, and you can then copy/paste the text back into Word.

Signature

Suzanne S. Barnhill
Microsoft MVP (Word)
Words into Type
Fairhope, Alabama USA
Word MVP FAQ site: http://word.mvps.org
Email cannot be acknowledged; please post all follow-ups to the newsgroup so
all may benefit.

> Hi everyone,
>
[quoted text clipped - 3 lines]
> Thank you,
> Cooz
Tesla - 28 Oct 2006 16:50 GMT
One more restriction to that approach: unfortunately that does not handle all
the things that can be in a cell, such as Objects (see MS Equation) or
captions,  when one goes from Word to Excel.

The quest continues.

> Although "macropod" has given you the options within Word, there is a way to
> transpose the rows and columns, but it will work only if the table is fairly
[quoted text clipped - 10 lines]
> > Thank you,
> > Cooz
Helmut Weber - 05 Jun 2006 18:59 GMT
Hi Cooz,

>Is there a way to turn a Word table
>so that its columns become rows and its
>rows become columns? If yes... how?

yes and no,

simple only if the number of columns equals
the number of rows.
Otherwise you have to create a new table, based on the
data from the old table.

Rows x columns has always to be the same.

Fancy things like embedded tables,
split and merged cells, embedded objects etc.
would make it very difficult.

The principle for cells containing nothing but text
and regardless of formatting, is:

Sub Test0004()
Dim lCll As Long ' number of cells
Dim l As Long    ' just a counter
lCll = ActiveDocument.Tables(1).Range.Cells.Count
' create an array representing all the cells
' from left to right for each row
ReDim aCell(1 To lCll) As String
For l = 1 To lCll
  aCell(l) = ActiveDocument.Tables(1).Range.Cells(l).Range.Text
Next
' now you've got all text from all cells
' in a one dimensional array
' and can arrange the data as you please
End Sub

Theory only.
Get Excel to help you, as Suzanne recommended.

Signature

Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"

Cooz - 06 Jun 2006 15:09 GMT
Thank you, macropod, Suzanne and Helmut for your response.

Cooz

> Hi everyone,
>
[quoted text clipped - 3 lines]
> Thank you,
> Cooz
Bob S - 22 Jun 2006 20:53 GMT
I was looking at this and found a way to do it entirely in Word with
no macros. It is a bit tedious if the table has a lot of columns
though...

Make some spare space below your table with empty paragraphs. Select
and copy the first column of the table and paste it into the spare
space, giving you a one-column table. Select and copy the second
column of the original table and paste it immediately below the new
table, giving you a longer one column table. Keep doing this until you
have done every column in the original table. Now select the new
one-column table, convert it to text, and convert it back to a table
with the proper number of columns (i.e. the original number of rows).

Bob S

>Hi everyone,
>
[quoted text clipped - 3 lines]
>Thank you,
>Cooz
Cooz - 23 Jun 2006 15:28 GMT
Hi Bob,

That's one fine solution. Thank you!

Cooz

> I was looking at this and found a way to do it entirely in Word with
> no macros. It is a bit tedious if the table has a lot of columns
[quoted text clipped - 18 lines]
> >Thank you,
> >Cooz
Tesla - 26 Oct 2006 02:56 GMT
Yes, there is. It involves a small macro that the vendor does not ship with
the product. I am sorry that this forum danced around the issue and gave you
no good advice.

This is a complete solution, which handles multiple tables selected, of any
size, even with embedded OLE objects in the cells, and keeps the original
formatting of the table. I worked Equations objects successfully in them.

Do not panic with the seemingly chaotic dance of tables and selections in
the document during processing, it all works nicely.

Here it is:
Option Explicit
' Author: Sidney, like I need to give a last name!
Sub TransposeTablesSelected()
   Dim t As Table
   For Each t In Selection.Tables
       TransposeTable t
   Next
End Sub
Sub TransposeTable(t As Table)
   Dim original_rows As Integer
   original_rows = t.Rows.Count
   
   Dim original_cols As Integer
   original_cols = t.Columns.Count
   
   Dim diagonal_count As Integer
   diagonal_count = IIf(original_rows > original_cols, original_rows,
original_cols)
   
   ' increase size
   Do While t.Rows.Count < diagonal_count
       t.Rows.Add
   Loop
   Do While t.Columns.Count < diagonal_count
       t.Columns.Add
   Loop
           
   ' Auxiliary holder of table cell
   Selection.MoveUp Unit:=wdLine, Count:=1
   Dim auxTable As Table
   Set auxTable = ActiveDocument.Tables.Add(Range:=Selection.Range,
NumRows:=2, NumColumns:= _
       2, DefaultTableBehavior:=wdWord9TableBehavior, AutoFitBehavior:= _
       wdAutoFitContent)
                   
   Dim j As Integer
   Dim k As Integer
   For k = 1 To diagonal_count
       For j = k + 1 To diagonal_count
           Call TableCellSwap(t, k, j, auxTable, j, k)
       Next
   Next
   
   auxTable.Delete
   
   ' decrease size
   Do While t.Rows.Count > original_cols
       t.Rows(t.Rows.Count).Delete
   Loop
   Do While t.Columns.Count > original_rows
       t.Columns(t.Columns.Count).Delete
   Loop
End Sub
Sub TableCellMove(sourceTable As Table, sourceRow As Integer, sourceCol As
Integer, destTable As Table, destRow As Integer, destCol As Integer)
   sourceTable.Cell(sourceRow, sourceCol).Select
   Selection.Cut
       
   destTable.Cell(destRow, destCol).Select
   Selection.Paste
End Sub
Sub TableCellSwap(sourceTable As Table, sourceRow As Integer, sourceCol As
Integer, auxTable As Table, destRow As Integer, destCol As Integer)
   Call TableCellMove(sourceTable, destRow, destCol, auxTable, 1, 1)
   Call TableCellMove(sourceTable, sourceRow, sourceCol, sourceTable,
destRow, destCol)
   Call TableCellMove(auxTable, 1, 1, sourceTable, sourceRow, sourceCol)
End Sub

> Hi everyone,
>
[quoted text clipped - 3 lines]
> Thank you,
> Cooz
Pat Garard - 26 Oct 2006 11:32 GMT
Hi Sweetheart,

> Yes, there is. It involves a small macro that the vendor does not ship with ..
Thank you SO MUCH for a lesson in matrix transposition. Of course the
Vendor actually shipped the solution in Excel ...

99.9% of Word Users do not need matrices ...
99.9% of Excel Users MIGHT ....

I feel so ... enriched ...
Signature

Regards,
Pat Garard
Melbourne, Australia
_______________________

> Yes, there is. It involves a small macro that the vendor does not ship with
> the product. I am sorry that this forum danced around the issue and gave you
[quoted text clipped - 84 lines]
>> Thank you,
>> Cooz
Tesla - 27 Oct 2006 03:19 GMT
Your tone is not welcome. And you have not contributed to a solution.

This is a discussion about transposing tables in Word;
not matrices, not Excel documents.

99.9% of statistics are made up by the people that quote them.

> Hi Sweetheart,
>
[quoted text clipped - 6 lines]
>
> I feel so ... enriched ...
macropod - 26 Oct 2006 12:15 GMT
> I am sorry that this forum danced around the issue and gave you
> no good advice.

Really? The other solutions offered all do the job in various ways, at least
one of which met the OP's needs.

Your code needs more work. I tried it with a 5-column, 4-row table and
nothing was transposed. Plus, if the table is at the very top of the
document, the code crashes.

Cheers

Signature

macropod
[MVP - Microsoft Word]

Tesla - 27 Oct 2006 03:10 GMT
Thanks for reporting the problem with the macro.
Please see second posting, which addresses the problem by not using an
auxiliary table.

> > I am sorry that this forum danced around the issue and gave you
> > no good advice.
[quoted text clipped - 7 lines]
>
> Cheers
Tesla - 27 Oct 2006 03:13 GMT
Everyone thank macropod for alpha testing the macro, here is version 2, much
improved:

Option Explicit
Sub TransposeTableSelected()
   Dim c As New Collection
   Dim o
   For Each o In Selection.Tables
       c.Add o
   Next
   Dim t As Table
   For Each t In c
       TransposeTable t
   Next
End Sub
Sub TransposeTable(t As Table)
   If t Is Nothing Then
       Exit Sub
   End If
   Dim original_rows As Integer
   original_rows = t.Rows.Count
   
   Dim original_cols As Integer
   original_cols = t.Columns.Count
   
   Dim diagonal_count As Integer
   diagonal_count = IIf(original_rows > original_cols, original_rows,
original_cols)
   
   ' increase size
   Do While t.Rows.Count < diagonal_count
       t.Rows.Add
   Loop
   Do While t.Columns.Count < diagonal_count
       t.Columns.Add
   Loop
   ' add a row to hold values as they are shuffled.
   t.Rows.Add
                   
   Dim j As Integer
   Dim k As Integer
   For k = 1 To diagonal_count
       For j = k + 1 To diagonal_count
           Call TableCellSwap(t, k, j, j, k)
       Next
   Next
   
   ' decrease size
   Do While t.Rows.Count > original_cols
       t.Rows(t.Rows.Count).Delete
   Loop
   Do While t.Columns.Count > original_rows
       t.Columns(t.Columns.Count).Delete
   Loop
End Sub
Sub TableCellMove(sourceTable As Table, sourceRow As Integer, sourceCol As
Integer, destTable As Table, destRow As Integer, destCol As Integer)
   sourceTable.Cell(sourceRow, sourceCol).Select
   Selection.Cut
       
   destTable.Cell(destRow, destCol).Select
   Selection.Paste
End Sub
Sub TableCellSwap(sourceTable As Table, sourceRow As Integer, sourceCol As
Integer, destRow As Integer, destCol As Integer)
   Call TableCellMove(sourceTable, destRow, destCol, sourceTable,
sourceTable.Rows.Count, 1)
   Call TableCellMove(sourceTable, sourceRow, sourceCol, sourceTable,
destRow, destCol)
   Call TableCellMove(sourceTable, sourceTable.Rows.Count, 1, sourceTable,
sourceRow, sourceCol)
End Sub

> Yes, there is. It involves a small macro that the vendor does not ship with
> the product. I am sorry that this forum danced around the issue and gave you
[quoted text clipped - 8 lines]
>
> Here it is:

see version 2 above.
macropod - 27 Oct 2006 12:17 GMT
Still needs more work - bombs on tables with merged cells.

Cheers

Signature

macropod
[MVP - Microsoft Word]

> Everyone thank macropod for alpha testing the macro, here is version 2, much
> improved:
[quoted text clipped - 83 lines]
>
> see version 2 above.
 
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.