Microsoft Word VBA Table Cell Height: A Complete Guide
In this article
To change the height of a table cell using Microsoft Word VBA, you must target the HeightRule and Height properties of the Row object, as cell height is governed by the row it belongs to. The most direct method is to set ActiveDocument.Tables(1).Rows(1).HeightRule = wdRowHeightExactly and then define the ActiveDocument.Tables(1).Rows(1).Height = CentimetersToPoints(1). This ensures the row remains at a fixed size regardless of the content inside.
What You'll Need
- Microsoft Word installed on a Windows or macOS environment.
- A document containing at least one table.
- The Developer tab enabled in the Word Ribbon (File > Options > Customize Ribbon > check Developer).
- Basic familiarity with the Visual Basic for Applications (VBA) editor.
Step-by-Step
- Open the VBA Editor: With your Word document open, press
Alt + F11on your keyboard. This opens the Microsoft Visual Basic for Applications window. - Insert a New Module: In the Project Explorer pane on the left, right-click on your document name (e.g., Project (Document1)), select
Insert, and then clickModule. This creates a blank space where you can write your custom macro. - Define the Subroutine: Type
Sub SetTableCellHeight()at the top of the module to begin the macro. - Declare Your Variables: To make the code clean and readable, declare variables for your table and row. Type:
Dim myTable As TableDim myRow As Row - Assign the Table and Row: Specify which table you are targeting. If it is the first table in the document, use:
Set myTable = ActiveDocument.Tables(1)Set myRow = myTable.Rows(1) - Set the Height Rule: This is the most critical step. Word has different rules for how it handles height. To force a specific height, you must use
wdRowHeightExactly. Type:myRow.HeightRule = wdRowHeightExactlyIf you prefer the row to grow if the text exceeds the set height, usewdRowHeightAtLeast. - Apply the Height Value: Word measures distances in points. To use more intuitive measurements like centimeters or inches, use the built-in conversion functions. For a height of 1.5 centimeters, type:
myRow.Height = CentimetersToPoints(1.5)Alternatively, for inches, useInchesToPoints(0.5). - Close and Run: Press
Ctrl + Sto save, then close the VBA window. Go back to your Word document, pressAlt + F8, selectSetTableCellHeightfrom the list, and clickRun.
Common Mistakes
One of the most frequent errors is attempting to set the Height property without first defining the HeightRule. If the rule is set to wdRowHeightAuto (the default), Word will ignore your specific point value and simply shrink or expand the row to fit the text. Always explicitly set the rule to wdRowHeightExactly or wdRowHeightAtLeast before assigning a numerical value.
Another common mistake is confusing Cell objects with Row objects. In Word's object model, a Cell does not have its own independent height; it is a child of a Row. If you try to call ActiveDocument.Tables(1).Cell(1, 1).Height, the VBA compiler will return an error because the height property belongs to the row, not the individual cell. You must reference the row containing that cell using ActiveDocument.Tables(1).Cell(1, 1).Row.Height.
Users also frequently forget that Word's internal measurement system is based on points (1/72 of an inch). Entering a number like "2" expecting 2 centimeters will result in a row that is only 2 points high, which is virtually invisible. Always use conversion functions like CentimetersToPoints or InchesToPoints to ensure accuracy.
Tips
To apply a uniform height to every row in a table, avoid writing a line of code for every single row. Instead, use a For Each loop. This allows you to iterate through the entire Rows collection of the table. For example:
For Each myRow In myTable.Rows
myRow.HeightRule = wdRowHeightExactly
myRow.Height = CentimetersToPoints(1)
Next myRow.
If you are working with complex tables that contain merged cells, be cautious. Merged cells can break the Rows collection, causing VBA to throw a "Requested member of the collection does not exist" error. In such cases, you cannot use the Rows object directly. Instead, you must iterate through the Cells collection and access the row via the cell: myTable.Cell(i, j).Row.Height.
For a more professional look, consider combining height adjustments with vertical alignment. Once you have set a large exact height, the text will default to the top of the cell. To center it vertically, use:
myRow.Cells.VerticalAlignment = wdCellAlignVerticalCenter.
Troubleshooting
If your code runs without errors but the row height does not change, check if the table has "Prefer window" or "AutoFit to contents" settings enabled. While VBA usually overrides these, sometimes conflicting table properties can cause issues. Try selecting the table and ensuring that the layout is not locked.
If you encounter a "Run-time error 5941: The requested member of the collection does not exist," it is almost certainly due to merged cells. As mentioned in the tips, Word's Rows collection becomes unstable when cells are merged across rows or columns. To fix this, stop using myTable.Rows(i) and instead use a loop that targets myTable.Cell(r, c).Row. This ensures that VBA finds the row object associated with that specific coordinate, even if the row structure is non-uniform.
If the row height is larger than you specified, check the font size and paragraph spacing of the text inside the cell. If the HeightRule is set to wdRowHeightAtLeast, Word will expand the row to accommodate the font size, line spacing, and any "Space After" or "Space Before" paragraph settings. To achieve a strict height, you must use wdRowHeightExactly and potentially reduce the font size or paragraph spacing to prevent text from being clipped.
Frequently Asked Questions
Can I set different heights for different cells in the same row?
No. In Microsoft Word, height is a property of the row, not the cell. Every cell in a single row must share the same height. If you need cells to appear as if they have different heights, you must use merged cells or nested tables.
What is the difference between wdRowHeightExactly and wdRowHeightAtLeast?
wdRowHeightExactly forces the row to be the exact measurement specified, even if the text inside is too large (which will result in the text being cut off). wdRowHeightAtLeast sets a minimum height; if the text exceeds that height, the row will automatically expand to fit the content.
How do I convert points to centimeters in VBA?
While Word provides CentimetersToPoints, it does not provide a direct PointsToCentimeters function. To convert back, you can use the mathematical constant: 1 centimeter is approximately 28.35 points. You can divide the point value by 28.35 to find the centimeter equivalent.
Will this VBA code work on Word for Mac?
Yes, the Table, Row, and HeightRule objects are part of the standard Word object model and are compatible with both Windows and macOS versions of Word, provided the VBA environment is enabled.
How do I apply height to only the first row (header row)?
You can target the first row specifically by using the index 1 in the rows collection: ActiveDocument.Tables(1).Rows(1).Height = CentimetersToPoints(1.2). This is commonly used to make header rows more prominent than data rows.