VBA & Macro Problems

Automatically Adjust Excel Row Height for Cells with Line Breaks

Natalie TaylorNatalie Taylor Nov 14, 2026 868 views

Question details

Adjust row heights for cells containing line-feed characters within print areas on several Excel worksheets.

Automatically Adjust Excel Row Height for Cells with Line Breaks
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA editor

Open the workbook in desktop Excel, then select Developer > Visual Basic. If the Developer tab is hidden, enable it in Excel's ribbon settings.

2
Insert the macro

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

3
Save and run the macro

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.

4
Check the printed layout

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.

Use VBA to adjust rows containing line feeds in print areas
Print area and row scope: Sheets without a defined print area are skipped. Row height affects the entire row, including cells outside the print area.
Free Microsoft Office alternative

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.

Wrap cell text and adjust row heights in WPS SpreadsheetsOpen and edit Microsoft Excel XLSX filesUse a familiar interface for an easier move from OfficeWork with a free, lightweight office suite
microsoft office alternative - wps office

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.