PDA

View Full Version : Reverse Column Order in Table



YossiD
08-03-2023, 12:52 AM
I have received some Word tables that were mistakenly created with Table direction as Right-to-left. As soon as I change to Left-to-right, all of the columns get reversed - first column on the left becomes the last column on the right, etc.

I'm sure there's a relatively simple way to put all of the columns back where they belong using a macro, but so far I've had only partial success. Can someone help with this? Of course all of the column widths must be kept as is.

Thanks

Aussiebear
08-03-2023, 04:31 AM
Maybe try the following:

Insert a new row in Row 1, and number the cells sequentially (1 to how ever many),to cover the columns used in your worksheet.
Highlight all the Used range;
Select Sort then Options;
Select "Sort from Left to Right" and click OK;
Make sure Sort by options show Row 1 and Largest to smallest, then click on OK.

YossiD
08-03-2023, 04:44 AM
Hi Aussibear and thanks for your reply.

I am in Word 2013, and I don't see any "Sort from Left to Right" option. I suppose it was added in a later version of Word.

With the help of Bing's Ai Chat, I now have this macro that almost works. It copies the first column on the left and pastes it as the last column on the right. The problem is that it pastes the contents of all rows in the cut column into a single cell (top) of the new column.



Sub ReverseColumns()
Dim tbl As Table
Dim col As Column
Dim i As Long

Set tbl = Selection.Tables(1)

Set col = tbl.Columns(1)
col.Select
Selection.Cut
tbl.Columns.Add
tbl.Columns(tbl.Columns.Count).Cells(1).Select
Selection.PasteSpecial DataType:=wdPasteText

End Sub


Once I get this working properly, I should be able to get all of the columns to paste in the desired order.

YossiD
08-03-2023, 04:51 AM
Of course, I found the mistake as soon as I finished posting.

The culprit is
tbl.Columns(tbl.Columns.Count).Cells(1).Select.
Replacing it with
tbl.Columns(tbl.Columns.Count).Select. works fine.

YossiD
08-07-2023, 05:20 AM
I'm still not managing with this.

Can someone help me with a macro that will reverse column order left-to-right, while preserving column widths and all text formatting?

Thanks

Aussiebear
08-07-2023, 06:46 AM
You could simply save a second version and reverse the column layout to suit yourself and have the original left as the non amended version.