View Single Post
  #4   Report Post  
Posted to microsoft.public.word.tables
Helmut Weber Helmut Weber is offline
external usenet poster
 
Posts: 139
Default Convert vertical table to horizontal?

Hi,

if you know how to get a macro to work,
you might try this one,
which works for simple tables, as far as I see.

Simple means, no split or merged cells,
no table containing other tables,
no table containing objects like pictures etc.

Sub TransposeTable()
Dim C As Long ' column
Dim R As Long ' row

Dim x As Long ' just a counter
Dim y As Long ' just a counter
Dim z As Long ' just a counter

Dim STempo As String
Dim sArr() As String

With Selection
.Collapse
If Not .Information(wdWithInTable) Then
MsgBox "insertion point not in table"
Exit Sub
End If

With .Tables(1)
C = .Columns.Count
R = .Rows.Count
If R * C .Range.Cells.Count Then
MsgBox "split or merged cells"
Exit Sub
End If
ReDim sArr(1 To .Range.Cells.Count)
For x = 1 To UBound(sArr)
STempo = .Range.Cells(x).Range.Text
STempo = Left(STempo, (Len(STempo) - 2))
sArr(x) = STempo
Next
' --------------------------
With .Columns
For x = 1 To R - C
.Add
Next
For x = 1 To C - R
.Last.Delete
Next
End With
' --------------------------
With .Rows
For x = 1 To R - C
.Last.Delete
Next
For x = 1 To C - R
.Add
Next
End With
'--------------------------
' Transpose
'--------------------------
C = .Columns.Count
R = .Rows.Count
x = 0
For y = 1 To C
For z = 1 To R
x = x + 1
.Cell(z, y).Range.Text = sArr(x)
Next
Next
End With
End With

End Sub

HTH nevertheless

--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

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