4

Is there a way to transpose (swap rows and columns) in post-ribbon Microsoft Word (currently using 2013)?

For example, in this table, I want the top headings to go on the left (én, te, etc), and the left headings (van, megy, jön) go to the top (obviously with the data moved also) table to be transposed

I currenly copy and paste into Excel, copy and paste again but choose the special paste option : Transpose, then copy back to word. But is there a proper way to do this natively?

transpose menu

Judith
  • 673
  • 5
  • 18
Jay Wick
  • 6,817

3 Answers3

1

Try this macro:

Sub transpose01()
'
' transpose01 Macro
'
'
' to transpose rows and columns in a table
Dim NumCols As Long, NumRows As Long, RowCounter As Long, ColCounter As Long
Dim CellText As String
NumCols = ActiveDocument.Tables(1).Columns.Count
NumRows = ActiveDocument.Tables(1).Rows.Count
ActiveDocument.Tables.Add Range:=Selection.Range, NumRows:=NumCols, NumColumns:=NumRows
RowCounter = 0
While RowCounter < NumRows
ColCounter = 0
While ColCounter < NumCols
CellText = ActiveDocument.Tables(1).Cell(RowCounter + 1, ColCounter + 1).Range.Text
CellText = Left(CellText, Len(CellText) - 2)
ActiveDocument.Tables(2).Cell(ColCounter + 1, RowCounter + 1).Range.InsertBefore CellText
ColCounter = ColCounter + 1
Wend
RowCounter = RowCounter + 1
Wend '

End Sub
phuclv
  • 30,396
  • 15
  • 136
  • 260
1

I think using Excel is the only way.

András
  • 576
0

Here is a macro that works by copy-pasting each cell individually. This way it worked for me with equations in almost all cells.

I'm on Word Version 2301 (from Microsoft 365, probably whatever is the newest version in 2024). To be safe, I copy the table, paste it into a new empty document, then run the macro there.

Sub TransposeTable()
    Dim tbl As Table
    Dim newTbl As Table
    Dim i As Long, j As Long
    Dim rowCount As Long, colCount As Long
    Dim rng As Range
' Get the first table in the document
Set tbl = ActiveDocument.Tables(1)

' Get the number of rows and columns
rowCount = tbl.Rows.Count
colCount = tbl.Columns.Count

' Set the range after the original table
Set rng = tbl.Range
rng.Collapse Direction:=wdCollapseEnd

' Insert two new lines after the original table to avoid them sticking together
rng.InsertAfter vbCrLf &amp; vbCrLf
rng.Collapse Direction:=wdCollapseEnd

' Add a new table with transposed dimensions at the end of the original table
Set newTbl = ActiveDocument.Tables.Add(Range:=rng, NumRows:=colCount, NumColumns:=rowCount)

' Loop through each cell in the original table and copy-paste it to the transposed table
For i = 1 To rowCount
    For j = 1 To colCount
        ' Copy the cell content from the original table
        tbl.Cell(i, j).Range.Copy

        ' Paste the content into the transposed table
        newTbl.Cell(j, i).Range.Paste
    Next j
Next i

' Optional: Delete the original table
' tbl.Delete

End Sub

MikeA
  • 1