Consulting

Results 1 to 3 of 3

Thread: Autofit a Table Width

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    VBAX Contributor
    Joined
    Dec 2018
    Location
    South London
    Posts
    115
    Location

    Autofit a Table Width

    Hi John / everyone

    I've tried to make code that narrows all columns in a table to auto fit their text. But it goes one character wide per column.
    Any ideas please? So columns will narrow to the width of its contents (unless text is wrapping of course, then that width is fine).
    Thus the tables would be narrower in total width. If I change otbl.Parent.Width = 0 to Auto, it still fails. (Parent.Height = 0 works fine for table heights.)


    Sub NarrowColumnWidths()
    
    
    Dim minW As Single
    With ActiveWindow.Selection.ShapeRange(1).Table
    Set otbl = ActiveWindow.Selection.ShapeRange(1).Table
    For iCol = 1 To .Columns.Count
    For iRow = 1 To .Rows.Count
    With .Cell(iRow, iCol).Shape.TextFrame
    otbl.Parent.Width = 0
    End With
    Next
    Next
    End With
    
    
    End Sub

    No sure if it's possible, I know you can in Excel and Word, just hoping this may do it?

    Thank you.
    Last edited by Zack Barresse; 04-08-2020 at 10:46 AM. Reason: added proper CODE tags

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
  •