In Microsoft Excel, the AutoFit feature does not work with merged cells, meaning that row heights won't automatically adjust to fit the content when cells are merged and text wrapping is enabled[1].
To address this limitation, you can use a VBA (Visual Basic for Applications) macro to automatically adjust the row height of merged cells. Here's a VBA macro that accomplishes this:
Sub AutoFitMergedCellRowHeight()
Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
Dim CurrCell As Range
Dim ActiveCellWidth As Single, PossNewRowHeight As Single
Dim iX As Integer
If ActiveCell.MergeCells Then
With ActiveCell.MergeArea
If .Rows.Count = 1 And .WrapText = True Then
Application.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth = ActiveCell.ColumnWidth
For Each CurrCell In Selection
MergedCellRgWidth = CurrCell.ColumnWidth + MergedCellRgWidth
iX = iX + 1
Next
MergedCellRgWidth = MergedCellRgWidth + (iX - 1) * 0.71
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, CurrentRowHeight, PossNewRowHeight)
End If
End With
End If
End Sub
To implement this macro:
- Press
ALT + F11 to open the VBA editor.
- In the editor, go to
Insert > Module to create a new module.
- Copy and paste the above code into the module.
- Close the VBA editor.
- Back in Excel, select the merged cell you want to adjust.
- Press
ALT + F8, select AutoFitMergedCellRowHeight, and click Run.
This macro unmerges the selected cell, adjusts the column width to fit the content, auto-fits the row height, and then re-merges the cells, effectively resizing the row to display all the text[2].
Alternative Workaround:
If you prefer not to use macros, you can manually adjust the row height or use a helper column:
- In an unused column (e.g., column Z), enter a reference to the merged cell (e.g.,
=A1).
- Ensure this helper cell has the same formatting as the merged cell, except it should not be merged.
- Set the column width of the helper cell to match the combined width of the merged cells.
- Use the AutoFit feature on the row containing the helper cell.
This method allows Excel to calculate the appropriate row height based on the helper cell, which mirrors the content and width of the merged cell[3].
By using either the VBA macro or the helper column method, you can effectively manage row heights for merged cells in Excel.