How to auto fit row height of merged cells in Excel?
In Excel, we can quickly adjust the row height to the fit the cell contents by using the AutoFit Row Height feature, but this function will completely ignores merged cells. That is to say, you can’t apply the AutoFit Row Height feature to resize the row height of merged cells, you need to manually adjust the row height for merged cells one by one. In this article, I can introduce some quick methods to solve this problem.
- Reuse Anything: Add the most used or complex formulas, charts and anything else to your favorites, and quickly reuse them in the future.
- More than 20 text features: Extract Number from Text String; Extract or Remove Part of Texts; Convert Numbers and Currencies to English Words.
- Merge Tools: Multiple Workbooks and Sheets into One; Merge Multiple Cells/Rows/Columns Without Losing Data; Merge Duplicate Rows and Sum.
- Split Tools: Split Data into Multiple Sheets Based on Value; One Workbook to Multiple Excel, PDF or CSV Files; One Column to Multiple Columns.
- Paste Skipping Hidden/Filtered Rows; Count And Sum by Background Color; Send Personalized Emails to Multiple Recipients in Bulk.
- Super Filter: Create advanced filter schemes and apply to any sheets; Sort by week, day, frequency and more; Filter by bold, formulas, comment...
- More than 300 powerful features; Works with Office 2007-2019 and 365; Supports all languages; Easy deploying in your enterprise or organization.
Supposing I have a worksheet with some merged cells as following screenshot shown, and now I need to resize the cell row height to display the whole contents, the below VBA code may help you to auto fit the row height of multiple merged cells, please do as follows:
1. Hold down the ALT + F11 keys, and it opens the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.VBA code: Auto fit row height of multiple merged cells
Option Explicit Public Sub AutoFitAll() Call AutoFitMergedCells(Range("a1:b2")) Call AutoFitMergedCells(Range("c4:d6")) Call AutoFitMergedCells(Range("e1:e3")) End Sub Public Sub AutoFitMergedCells(oRange As Range) Dim tHeight As Integer Dim iPtr As Integer Dim oldWidth As Single Dim oldZZWidth As Single Dim newWidth As Single Dim newHeight As Single With Sheets("Sheet4") oldWidth = 0 For iPtr = 1 To oRange.Columns.Count oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth Next iPtr oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth oRange.MergeCells = False newWidth = Len(.Cells(oRange.Row, oRange.Column).Value) oldZZWidth = .Range("ZZ1").ColumnWidth .Range("ZZ1") = Left(.Cells(oRange.Row, oRange.Column).Value, newWidth) .Range("ZZ1").WrapText = True .Columns("ZZ").ColumnWidth = oldWidth .Rows("1").EntireRow.AutoFit newHeight = .Rows("1").RowHeight / oRange.Rows.Count .Rows(CStr(oRange.Row) & ":" & CStr(oRange.Row + oRange.Rows.Count - 1)).RowHeight = newHeight oRange.MergeCells = True oRange.WrapText = True .Range("ZZ1").ClearContents .Range("ZZ1").ColumnWidth = oldZZWidth End With End Sub
(1.) In the above code, you can add new ranges just copy Call AutoFitMergedCells(Range("a1:b2")) script many times as you want, and change the merged cell ranges to your needed.
(2.) And you should change the current worksheet name Sheet4 to your used sheet name.
3. Then press F5 key to run this code, and now, you can see all the merged cells have been auto fitted to their cell contents, see screenshot: