Macro won't format table

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Macro won't format table

Post by Peter Kinross »

The attached table will not format using this macro.

Code: Select all

Sub Macro2()
    With Selection.Cells(1)
        .TopPadding = CentimetersToPoints(0.05)
        .BottomPadding = CentimetersToPoints(0.05)
        .LeftPadding = CentimetersToPoints(0.05)
        .RightPadding = CentimetersToPoints(0.05)
        .WordWrap = True
        .FitText = False
    End With
End Sub
If I delete the right column the table appears as I want without even using the macro.
I'm stumped.
You do not have the required permissions to view the files attached to this post.
Avagr8day, regards, Peter

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro won't format table

Post by HansV »

Which cell should I select before running the macro?
Best wishes,
Hans

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: Macro won't format table

Post by Peter Kinross »

The whole table. However the top and bottom rows usually seem to be fine, so I also try selecting all rows between those. Neither selections work.
Avagr8day, regards, Peter

User avatar
HansV
Administrator
Posts: 78446
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Macro won't format table

Post by HansV »

The macro will only affect the first cell in the selection, not the entire selection:

Code: Select all

    With Selection.Cells(1)
So try this:

Code: Select all

Sub Macro2()
    Dim c As Cell
    Application.ScreenUpdating = False
    Selection.Tables(1).Select
    For Each c In Selection.Cells
        With c
            .TopPadding = CentimetersToPoints(0.05)
            .BottomPadding = CentimetersToPoints(0.05)
            .LeftPadding = CentimetersToPoints(0.05)
            .RightPadding = CentimetersToPoints(0.05)
            .WordWrap = True
            .FitText = False
        End With
    Next c
    Application.ScreenUpdating = True
End Sub
Best wishes,
Hans

User avatar
Peter Kinross
5StarLounger
Posts: 962
Joined: 09 Feb 2010, 00:33
Location: Patterson Lakes, Victoria, Australia

Re: Macro won't format table

Post by Peter Kinross »

Oh boy, what a bewdy. In a million years, I would not have worked that one out.
ThanksHansStamp.gif
You do not have the required permissions to view the files attached to this post.
Avagr8day, regards, Peter