PDA

View Full Version : VBA to set cell margins to "Same as whole table"?



YossiD
03-14-2021, 02:13 AM
I want to set margins for all table cells to Same as the whole table, but I can't find the VBA function to do that. I thought maybe LeftPadding, RightPadding, etc. would do it, but I've not found a way to set them to the table default value. I also tried manually setting one cell and then using F4 to repeat for other cells, but that doesn't work either.

I have found that converting the table to text then back to table resets all cell margins to default, but in my particular case that messed up the table and I had to do considerable manual fixing-up.

A macro to set all table cells to Same as whole table would be great. Can it be done?

gmayor
03-14-2021, 03:55 AM
Padding should do it e.g. the following will apply the default padding, unless you have changed the default, in which case change the numbers to reflect what you have.


Sub PadAllCells()
Dim oCell As Cell
For Each oCell In Selection.Tables(1).Range.Cells
With oCell
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.19)
.RightPadding = CentimetersToPoints(0.19)
End With
Next oCell
Set oCell = Nothing
End Sub




Sub PadCurrentCell()
Dim oCell As Cell
Set oCell = Selection.Cells(1)
With oCell
.TopPadding = CentimetersToPoints(0)
.BottomPadding = CentimetersToPoints(0)
.LeftPadding = CentimetersToPoints(0.19)
.RightPadding = CentimetersToPoints(0.19)
End With
Set oCell = Nothing
End Sub

YossiD
03-14-2021, 07:05 AM
Thanks for the macros. They set cell padding per the specified values but they do not set the Same as the whole table checkbox, which is what I'm after. How can I do that?