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: Automatic Row Height For Merged Cells with Text Wrap.
Written by Allen Wyatt (last updated May 23, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Ernie asked if there was a way for Excel to automatically adjust the row height in cells that are merged. He points out that if a cell is set with text wrapping turned on, that Excel automatically adjusts the row height for the cell so that all the wrapped text is visible. If you subsequently merge that cell with an adjacent cell, even if the adjacent cell has text wrapping turned on, then the resulting merged cell's row height is not adjusted so that all the text is visible.
Exactly why Excel does this is unclear, but there is no intrinsic way around it—Excel just does it. At first blush you may think that you can use the AutoFit feature (Cells | Format | AutoFit or Format | Rows | AutoFit) to adjust the height of the row in which the merged cell is located. Doing so, however, apparently has no affect—AutoFit seems to completely ignore merged cells in doing its magic.
One way around the problem is to use a macro to set the row height to the desired height. A good approach is to have the macro determine the column width of the merged area, unmerge the columns, set the first column to that width, and determine the row height required to AutoFit it. The macro could then reset the column width, merge the cells, and set the new row height. (If that sounds like a lot, it is. Such a macro wouldn't be that trivial to create.)
If you don't want to use a macro, you can fool Excel into setting the row height properly. You do this by using a separate column that is created for the express purpose of setting row height. The process is described in the following general steps. (These steps assume you are merging the cells in columns A and B, and that your data table only includes columns A through K.)
The effect of these steps is that Excel will set the row height based upon the contents of column Z, which just happen to match the contents of the merged cell in columns A and B. This, of course, allows all the text in the merged cell to be visible. The only thing you will need to do is make sure that you don't print the contents of column Z when you do your printing.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10563) 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: Automatic Row Height For Merged Cells with Text Wrap.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
If you have some cells merged in a worksheet, and you wrap text within that merged cell, Excel won't automatically resize ...
Discover MoreWhen you enter information into a row on a worksheet, Excel automatically adjusts the height of the row based on what you ...
Discover MoreExcel allows you to easily hide rows in a worksheet, so their contents are not visible. Figuring out how to detect where ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2024-09-23 09:41:47
Rich
Tried this tip. The row does not automatically resize until you select the row and choose the autofit option or by doublclicking at the bottom of the row number.
The sheet is protected but the cells in the row are not. When autofit is applied the rows resize but if the content of the cell changes (to more or less), the row does not resize automatically to reflect the changes.
I'm sure I'm doing something wrong but it just doesn't do what is needed automatically.
2023-10-11 12:36:22
Wallace
Allen, thanks very much for this clever tip and your many other Word and Excel tips that have helped me over the years! Your clear writing style and your website's lack of distractions are also much appreciated.
2023-04-11 16:31:26
Gergő Gyula
Thank you, you saved my day, brilliant solution!
Don't forget to set the limitation to 255 on xW, because Excel will drop an error message on column width limitation.
2022-11-20 03:35:49
Alicia
Hi, this is a great tip. Is there any version of this for when you have rows that are merged instead of columns? If I do the above then I end up with the first merged row really big but then the other rows not changing, and if there is info in other columns for these rows then the merged one has a lot of white space. Thank you!!
2022-09-23 10:19:20
Basi
Thank you so much Allen for all your tips. My knowledge in Excel & Word have greatly improved. Much appreciated.
2022-05-10 12:31:00
ControlsGirl
Great work around!
2022-01-27 14:14:10
Oh so close for me, I 'm having the same issue with the non-macro method, as Christi from earlier in the comments.
When I enter the text into the merged cell and press enter the text wraps but the row height isnt updated unless I click back into the cell and press enter.
Anyone know how to get around that so that the row height sets when text is first entered into the cell.
Thanks
Kanishka
2021-10-19 17:59:46
Dan K
Brilliant! Thank you!
2021-09-21 11:42:49
J. Woolley
The macro suggested by this Tip is generalized in the following VBA:
Sub SetRowHeightByCell()
Dim rMA As Range, rCol As Range, rRow As Range
Dim nR As Long, xW As Double, xX As Double
With ActiveCell
Set rMA = .MergeArea
nR = rMA.Rows.Count
If .WrapText Then
For Each rCol In rMA.Columns
xW = xW + rCol.ColumnWidth
Next rCol
rMA.UnMerge
xX = .ColumnWidth
.ColumnWidth = xW
.Rows.AutoFit
.ColumnWidth = xX
xX = .RowHeight / nR
rMA.Merge
For Each rRow In rMA.Rows
rRow.RowHeight = xX
Next rRow
ElseIf nR = 1 Then
.Rows.AutoFit
Else
rMA.UnMerge
.Rows.AutoFit
xX = .RowHeight / nR
For Each rRow In rMA.Rows
rRow.RowHeight = xX
Next rRow
rMA.Merge
End If
End With
End Sub
Compare with the SetColumnWidthByCell macro described in my recent comment here:
https://excelribbon.tips.net/T011302_Widening_a_Column_to_a_Particular_Cells_Width.html
Both macros are available at My Excel Toolbox:
https://sites.google.com/view/MyExcelToolbox/
2021-09-17 16:01:25
Jonathan
Allen- long time appreciator of your tips. This one in particular helped me out of a problem that I had unsuccessfully tried to solve with VBA. Thank you for being so generous with your knowledge and know how.
2021-04-07 11:04:40
Enrique Celedón
Hi Allen, for years I've been trying to solve this, your solution works wonders ! Thanks a lot!!!
2020-11-25 02:39:42
Hans Hallebeek
Hi Allen,
I have been struggling with this for some time and your help column idea is great and works but I needed something for varying range widths.
I wrote a macro that loads a modeless userform with one textbox which is set to autofit and is filled with the text for that merged area, it then sets the textbox width to the merged area width.
The textbox height is used to calculate the row height using the font size of that range .
Sounds complicated but works; all I do is run the macro for those ranges.
2020-11-24 07:08:41
Girish
Hello Allen,
This is just so simple yet effective tip. This has helped me a lot.
Thanks.
BR,
Girish
2020-11-21 02:46:56
Hans Hallebeek
Hi, i've been trying to get this working too but do run into issues.
This is the Function I wrote and use:
Public Function count_TextLines(myText As Variant) As Integer
' Counts the lines by comparing the contents by the themselves after removing
' the line brakes which is character 10.
' This gives back the number of breaks so we add 1 to get the number of lines,
' since the last line doesn't have a line break.
count_TextLines = IIf(Len(Trim(myText)) = 0, 0, (Len(myText) - Len(Replace(myText, Chr(10), "")) + 2))
End Function
What is does is count the carriage feeds chr(10) and based on that returns the number of lines of text + 2, but that does not always work because the text passed is through a userform and when wordwrap is set the to true in the textbox the linefeed is not counted so what I really need ti find out is how much characters per line are needed for each line of text in a merged range before it wrap.
Probably count the characters and based upon the font size or something like that.
Thanks for reading
Hans
"IT" Always crosses your path...
2020-11-20 22:53:50
toowaki
The following software is an Excel add-in with the height adjustment function of merged cells.
Please download AutoFitRowEx from below URL.
https://github.com/toowaki/AutoFitRowEx/releases
To adjust the height, you need to copy the cell to another sheet, adjust the height, and then reflect that height in the actual cell height.
2020-10-17 10:59:18
Rocco
It's amazing how many shortcomings exist in Excel that have been ignored by Microsoft through hundreds or thousands of updates. I have been using this auto row height workaround for years, and I just shared the exact same steps with a friend before I found your post. However, I have found that Column Z needs to be slightly wider than the two merged cells combined. I surmised that Excel has some built-in cell margins or border widths that slightly skew the cell width. For example, I just merged two cells that measure 8 pixels each, but my Cell Z needed to be 16.2 pixels. I just drag the width of Z slightly until the contents wrap exactly like the merged cells. As the contents of the merged cells changes, the width of Z may need to be tweaked, but it can be perfected after a couple iterations.
2020-10-07 13:40:28
Matt
This approach only works if the source cells don't contain indents. Use the '=' approach drops the indents and the resulting row heights are off from the desired row heights.
2020-09-12 15:51:59
Anshu Kumar
I've found a working vba to solve this issue quickly here...https://www.mrexcel.com/board/threads/auto-adjust-row-height-merged-cells.556709/
Here is the code
Dim NewRwHt As Single
Dim cWdth As Single, MrgeWdth As Single
Dim c As Range, cc As Range
Dim ma As Range
Dim ProtectStatus As Boolean
With Target
If .MergeCells And .WrapText Then
ProtectStatus = Me.ProtectContents
If ProtectStatus Then Me.Unprotect "password"
Set c = Target.Cells(1, 1)
cWdth = c.ColumnWidth
Set ma = c.MergeArea
For Each cc In ma.Cells
MrgeWdth = MrgeWdth + cc.ColumnWidth
Next
Application.ScreenUpdating = False
On Error Resume Next
ma.MergeCells = False
c.ColumnWidth = MrgeWdth
c.EntireRow.AutoFit
NewRwHt = c.RowHeight
c.ColumnWidth = cWdth
ma.MergeCells = True
ma.RowHeight = NewRwHt
cWdth = 0: MrgeWdth = 0
On Error GoTo 0
Application.ScreenUpdating = True
If ProtectStatus Then Me.Protect "password"
End If
End With
This code works well for all the merged cell where text wrap is on.
However, I want to exclude some specific merged cell from autofit even when the wrap text is on.
For example, Range C5:C8 and Range E3:E7 are two merged cells with wrap text on, and I want to exclude these two merged cells from autofit.
Is it possible to do so by adjusting the above macro. Please help if possible! I'll really be thankful.
2020-07-08 13:05:59
Christi
This almost works but once I enter data into the merged cells, the content wraps but the row height does not immediately adjust. I have to double click the cell and press enter again. Then it works. Any thoughts?
2020-07-03 07:24:12
Thomas Meersschaert
Nice one
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