Consulting

Results 1 to 3 of 3

Thread: Autofit a Table Width

  1. #1
    VBAX Contributor
    Joined
    Dec 2018
    Posts
    103
    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

  2. #2
    Moderator VBAX Guru Aussiebear's Avatar
    Joined
    Dec 2005
    Location
    Queensland
    Posts
    3,862
    Location
    Have you tried to do this manually with the macro recorder active?
    Remember To Do the Following....
    Use 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 Wizard
    Joined
    Apr 2007
    Posts
    7,336
    Location
    Quote Originally Posted by Aussiebear View Post
    Have you tried to do this manually with the macro recorder active?

    One thing that PP lacks is a macro recorder similar to Excel's and Word's
    Last edited by Paul_Hossler; 04-09-2020 at 08:41 PM.
    ---------------------------------------------------------------------------------------------------------------------

    Paul


    Remember: Tell us WHAT you want to do, not HOW you think you want to do it

    1. Use [CODE] ....[/CODE ] Tags for readability
    [CODE]PasteYourCodeHere[/CODE ] -- (or paste your code, select it, click [#] button)
    2. Upload an example
    Go Advanced / Attachments - Manage Attachments / Add Files / Select Files / Select the file(s) / Upload Files / Done
    3. Mark the thread as [Solved] when you have an answer
    Thread Tools (on the top right corner, above the first message)
    4. Read the Forum FAQ, especially the part about cross-posting in other forums
    http://www.vbaexpress.com/forum/faq...._new_faq_item3

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
  •