How to Set Table Cell Widths and Distribute Evenly with Word VBA
Question details
The user needs a Word VBA macro to format a table by setting the first row's cell widths to 20%, 70%, and 10%, while evenly distributing selected cells in other specific rows.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Automating table layout formatting through VBA to ensure precise percentage-based widths and even distribution of specific cells.
- Observed behavior
- The user seeks a VBA solution that produces consistent, repeatable table dimensions without breaking the layout upon subsequent macro executions.
Ensure your document contains at least one table with the correct number of columns (e.g., three columns) and that the Developer tab is enabled in your ribbon to access the VBA editor.
Use VBA Macro to Adjust Table Layout via Percentages
Create and run a custom VBA macro that explicitly sets percentage widths for the first row and distributes remaining rows evenly, avoiding layout drift on repeated runs.
When manipulating table widths in Word VBA, using absolute points can cause the table boundaries to shift unexpectedly if the macro is run multiple times. To achieve consistent results, use the PreferredWidth property combined with wdPreferredWidthPercent.
By iterating through the specific cells of the first row to set exact percentages, and then targeting the rest of the table for even distribution, the layout becomes highly stable.
Press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.
In the left-hand Project pane, right-click your document name, hover over 'Insert', and select 'Module'.
Write your VBA code to target ActiveDocument.Tables(1). Iterate through the first row's cells setting Cell.PreferredWidthType = wdPreferredWidthPercent, assigning 20, 70, and 10 to Cell.PreferredWidth respectively.
For the subsequent rows, select the target rows using VBA and apply the Selection.Cells.DistributeWidth method to space them evenly.
Close the VBA editor, return to your document, and press ALT + F8. Select your new macro and click 'Run' to format the table.

Run Macros and Format Tables with WPS Writer
WPS Writer offers full compatibility with Microsoft Word documents and includes robust VBA support (in advanced editions), allowing you to execute macros for table formatting effortlessly.
- 1. Open your Document: Launch WPS Writer and open the document containing the table you want to format.
- 2. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click on 'Visual Basic' or 'Macros'.
- 3. Paste and Run: Paste your custom table-width VBA macro into the editor and click 'Run' to apply the formatting instantly.

Frequently Asked Questions
Why does my table width change inconsistently when running the macro multiple times?
If your macro uses absolute point values instead of percentage widths, the word processor may miscalculate boundaries on repeated runs. To fix this, always set the width type to wdPreferredWidthPercent for repeatable relative widths.
How do I distribute columns evenly without using VBA?
Select the columns or cells you want to adjust in your document, navigate to the 'Table Tools' > 'Layout' tab on the ribbon, and click the 'Distribute Columns' button.
How can I enable the Developer tab to write VBA code?
Go to 'File' > 'Options' > 'Customize Ribbon'. In the right-hand pane, check the box next to 'Developer' and click 'OK'. The Developer tab will now appear on your main ribbon.




