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.
Excel Productivity Tools
Office Tab: Bring powerful tabs to Office (include Excel), just like Chrome, Safari, Firefox and Internet Explorer. Save you half the time, and reduce thousands of mouse clicks for you. 30-day Unlimited Free Trial
Kutools for Excel: Save 70% of your time and solve 80% Excel problems for you. 300+ advanced features designed for 1500+ work scenario, make Excel much easy and increase productivity immediately.60-day Unlimited Free Trial
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:
Excel Productivity Tools
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
To post as a guest, your comment is unpublished.· 7 months agonot working , ye password set in your code not working in your code
To post as a guest, your comment is unpublished.· 9 months agoI believe the reason that the row heights do not calculate properly is related to these lines of code
For iPtr = 1 To oRange.Columns.Count
oldWidth = oldWidth + .Cells(1, oRange.Column + iPtr - 1).ColumnWidth
oldWidth = .Cells(1, oRange.Column).ColumnWidth + .Cells(1, oRange.Column + 1).ColumnWidth
The variable OldWidth gets set to the sum of the column widths in the range, but for some reason it gets reset to only the width of the first two columns. The first 3 lines of code are therefore made redundant by the 4th line. When I removed the line it was much better, but the other issue I found was that you have to make sure that the font and font size of the temporary cell (ZZ1 in the example code) must match the font and size of the merged cells; otherwise, text will not wrap in the same way as the merged cells wrap and may not be the correct height.
To post as a guest, your comment is unpublished.· 11 months agoI made add-in for Auto fit row height of multiple merged cells.
Please use this, if you want to autofit row hight.
［Release Ver2.6 · toowaki/AutoFitRowEx · GitHub］
To post as a guest, your comment is unpublished.· 1 years agoI am trying to understand the necessity of Line 19. You are assigning a value again to OldWidth. Can you please explain?
To post as a guest, your comment is unpublished.· 1 years agoMy code will not even run I just get a compile error when I try to call the AutoFitMergedCells - Expected Function or variable?