Automatically Adjust Excel Row Height for Cells with Line Breaks
Question details
Adjust row heights for cells containing line-feed characters within print areas on several Excel worksheets.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A workbook has multiple worksheets with two-line cell entries, and only cells within the defined print areas need to be checked.
- Observed behavior
- Rows containing line breaks need additional height so both lines display correctly.
In desktop Excel, check each sheet's print area under Page Layout > Print Area, and make sure the sheets are not protected. Test the macro on a copy of the workbook; use dummy data if you share a file for troubleshooting.
Use VBA to adjust rows containing line feeds in print areas
Run a macro across the workbook to wrap cells with line feeds and give their rows enough height for two lines.
Conditional formatting cannot change row height. The macro checks defined print areas for the line-feed character Chr(10), applies Wrap Text, and AutoFits affected rows without reducing their original height.
The minimum height below is 30 points; adjust it for your font and layout. Merged cells are skipped because AutoFit does not reliably size them.
Open the workbook in desktop Excel, then select Developer > Visual Basic. If the Developer tab is hidden, enable it in Excel's ribbon settings.
In the VBA editor, select Insert > Module and paste this code: Sub AdjustPrintAreaRows() Dim ws As Worksheet Dim cell As Range Dim oldHeight As Double Const minHeight As Double = 30 For Each ws In ThisWorkbook.Worksheets If Len(ws.PageSetup.PrintArea) > 0 Then For Each cell In ws.Range(ws.PageSetup.PrintArea).Cells If Not cell.MergeCells Then If Not IsError(cell.Value2) Then If InStr(1, CStr(cell.Value2), Chr(10), vbBinaryCompare) > 0 Then oldHeight = cell.EntireRow.RowHeight cell.WrapText = True cell.EntireRow.AutoFit If cell.EntireRow.RowHeight < oldHeight Then cell.EntireRow.RowHeight = oldHeight End If If cell.EntireRow.RowHeight < minHeight Then cell.EntireRow.RowHeight = minHeight End If End If End If End If Next cell End If Next ws End Sub
Use File > Save As to save the copy as an Excel Macro-Enabled Workbook (.xlsm). In Excel, select Developer > Macros, choose AdjustPrintAreaRows, and select Run. Enable macros only if you trust the workbook.
Open File > Print to preview each affected sheet. If needed, change the minHeight value in the module and run the macro again; adjust merged-cell rows manually.

Review wrapped spreadsheet rows in WPS Office
WPS Office is a free, lightweight alternative with a familiar spreadsheet interface. Open compatible Excel files and adjust wrapped rows as you migrate, although WPS does not fix Excel's VBA behavior.

Frequently Asked Questions
Can conditional formatting change row height in Excel?
No. Conditional formatting changes cell appearance but cannot set row height. Use a VBA macro or adjust rows through Home > Format > Row Height or AutoFit Row Height.
How does the macro detect a line break?
It uses InStr with Chr(10) to look for a line-feed character in each non-error cell within a defined print area.
Why did the macro skip a worksheet?
The macro skips sheets without a defined print area. Select the desired cells on that sheet and choose Page Layout > Print Area > Set Print Area before running it again.
Why is a merged cell still cut off?
The macro skips merged cells because Excel AutoFit may not size them reliably. Select the affected row and use Home > Format > Row Height to set its height manually.
Will the macro reduce an existing row height?
No. It restores the original height if AutoFit makes the row shorter, then applies a minimum of 30 points. You can change the minHeight value in the VBA module.




