Consulting

Results 1 to 6 of 6

Thread: Reverse Column Order in Table

  1. #1
    VBAX Regular YossiD's Avatar
    Joined
    Jan 2009
    Posts
    33
    Location

    Reverse Column Order in Table

    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

  2. #2
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

  3. #3
    VBAX Regular YossiD's Avatar
    Joined
    Jan 2009
    Posts
    33
    Location
    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.

  4. #4
    VBAX Regular YossiD's Avatar
    Joined
    Jan 2009
    Posts
    33
    Location
    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.

  5. #5
    VBAX Regular YossiD's Avatar
    Joined
    Jan 2009
    Posts
    33
    Location
    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

  6. #6
    Moderator VBAX Wizard Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    5,059
    Location
    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.
    Remember To Do the Following....
    Use [Code].... [/Code] tags when posting code to the thread.
    Mark your thread as Solved if satisfied by using the Thread Tools options.
    If posting the same issue to another forum please show the link

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •