VBA & Macro Problems

How to Set Table Cell Widths and Distribute Evenly with Word VBA

Huda QurayshiHuda Qurayshi Oct 26, 2026 869 views

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.

How to Set Table Cell Widths and Distribute Evenly with Word VBA
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the left-hand Project pane, right-click your document name, hover over 'Insert', and select 'Module'.

3
Apply Percentage Formatting Logic

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.

4
Distribute Remaining Cells Evenly

For the subsequent rows, select the target rows using VBA and apply the Selection.Cells.DistributeWidth method to space them evenly.

5
Run the Macro

Close the VBA editor, return to your document, and press ALT + F8. Select your new macro and click 'Run' to format the table.

Use VBA Macro to Adjust Table Layout via Percentages
Complex Script Debugging: Since Microsoft Community support generally handles installation and activation rather than custom development, consider posting highly complex reproducible code to developer forums like Stack Overflow with the 'VBA' tag if further debugging is needed.

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. 1. Open your Document: Launch WPS Writer and open the document containing the table you want to format.
  2. 2. Access the VBA Editor: Navigate to the 'Developer' tab on the ribbon and click on 'Visual Basic' or 'Macros'.
  3. 3. Paste and Run: Paste your custom table-width VBA macro into the editor and click 'Run' to apply the formatting instantly.
Seamless execution of Word VBA macrosFully compatible with .doc and .docx formatsAdvanced table layout and visual formatting tools
microsoft office alternative - wps office

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.