Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Adjusting Row Height when Wrapping Text.
Written by Allen Wyatt (last updated July 24, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Matt asked if there is a way to format a merged cell so that when the content of the cell exceeds the width, the row height is automatically adjusted to display the additional lines necessary.
Unfortunately, AutoFit (which Excel uses for row height) doesn't work with merged cells. There are ways around this problem, but none of them are easy or automatic. For this reason, you might consider re-designing your worksheet so that it doesn't use merged cells. If you must use them, then you might try this little trick, assuming the merged cells are in A2:M2:
You might wonder why, in step 4, you subtracted 2 from your original calculated width. This is just a "fudge factor" used to force a slightly narrower column width, and therefore slightly different word wrapping. This comes in handy when you are later viewing your worksheet using a different zoom factor or when you are using a different printer driver.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10228) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Adjusting Row Height when Wrapping Text.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
Excel automatically formats subtotals for you. But what if you want to change the default to something more suitable for ...
Discover MoreNeed to check the height of all the rows in a worksheet and then adjust them if a particular criterion is met? This tip ...
Discover MoreNeed to hide a large number of rows? It's easy to do if you combine a few keyboard shortcuts. Here are several techniques ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-07-26 04:16:59
Peter Atherton
As a matter of general interest Ed (in 2015) asked about calulating the combind width of cells. The following does this.
Function getWidth(ref As Range) As Integer
Dim c As Range, w As Long
For Each c In ref
w = w + Round(c.Width * 0.186, 0)
Next
getWidth = w
End Function
Entered as =GETWIDTH(C1:F1)
2020-07-25 10:34:17
Philip Andrew
Unfortunately, even for un-merged/single cells, AutoFit Row Height doesn’t seem to work. Something to do with cells that have undergone manual height adjustment at some point permanently inactivates AutoFit Row Height? Any help much appreciated.
2020-07-24 09:44:41
Kiwerry
@Allen: Thanks again for a useful site & newsletter - I enjoy the personal touch in the newsletters as well as the helpful tips.
As far as this tip is concerned, two comments:
1) a quick attempt showed that it may be possible to dispense with your steps 6 and 7 if, instead of putting the dummy cell (P55 in your example) way below and to the right of the data, put it on the same row as your merged cell but well over to the right of the data (P2 in your example). An auto-resize on the worksheet will set the correct height for this row. Reminder on auto-resize: Type Control-A to select the entire worksheet then double click with a double arrow on any column or row border. For some unknown reason, I had to use a fudge factor of +2 instead of Allen's -2 on the text I used if I wanted the appearance of A2:C2 to be the same as that of P2; otherwise the line breaks occurred in slightly different places.
2) If your merged cells are centre aligned, then consider using "Center across Selection" as the horizontal alignment instead of using "Merge and Center" on the cells.
@Kevin: I have also fallen into the trap of writing an answer on a post, then finding that the post was pretty old;-}} By the way, imo methods involving the number of characters (i.e. using the length function) only work well if you are using a non-proportional font like Courier.
@Ed: you could use a formula like =CELL("width",A2)+CELL("width",B2)+CELL("width",C2) to show you the combined widths of columns A to C, but a) the formula will only work where the cells in the row referred to are not merged (if they're merged you get a SPILL error, but the workaround is easy - just refer to a row which is outside your data), and b) if the problem only occurs once in a worksheet it may easier to just count and add.
2020-07-24 03:44:44
Kevin
Hi John - Read and created before looking at your date of enquiry so in the unlikely event that you or anyone else is still looking for this.
My example was based around adjusting the row height for text that was only either 1 or 2 rows of text.
You might be able to work your own solution based on these crude methods.
This example relates to - On the worksheet a formula to indicate the length of text (=LEN(V62) at Z62 Where the first cell of the merged cells was V62
Within your Macro parts
Dim r As Variant
' Make the Row Heights suit the text of the Prizes
r = "62" ' Row height to change
If Rows(r).EntireRow.Hidden = True Then
Else
If Sheets("Yours").Range("Z62").Value > 42 Then
Rows(r).RowHeight = 30
Else
Rows(r).RowHeight = 15
End If
End If
Good luck
2016-01-21 22:14:25
John Smith
Any way to programmatically calculate height of wrapped text? I need to do this because I programmatically insert pictures to rows and adjust row height to fit the picture. This way, pictures are displayed alright. But some rows are not tall enough to display wrapped text. I need a way to calculate and adjust the height of wrapped text so all text is displayed.
2015-07-28 10:26:35
Ed
What is the easiest way to (step 4) "calculate the combined width of all the cells that make up the merged cell"?
Given that I do not want to change the width of those columns. Is there anything faster than checking them each, one at a time?
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments