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.

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


20

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.)

  1. Merge the cells in columns A and B as desired.
  2. In column Z (or some other column outside of your data table range, but not immediately adjacent), put a simple formula reference to column A, as in =A1.
  3. Ensure the text formatting of column Z is exactly the same as in the merged cell, with the exception of merging. (Column Z should not be merged with anything, nor should it be marked as merged.)
  4. Make the width of column Z slightly narrower than the combined width of columns A and B.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Pulling Formulas from a Worksheet

The formulas in your worksheet can be displayed (instead of formula results) by a simple configuration change. You can ...

Discover More

Always Opening a Workbook that is Editable

When you send a workbook to a coworker, it can be bothersome if that person has problems using what you created. There is ...

Discover More

Hiding Macros

Don't want a particular macro to be visible in the Macros dialog box? Once you understand the criteria that Word uses ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Adjusting Row Height when Wrapping Text

If you have some cells merged in a worksheet, and you wrap text within that merged cell, Excel won't automatically resize ...

Discover More

Setting Row Height

When you enter information into a row on a worksheet, Excel automatically adjusts the height of the row based on what you ...

Discover More

Automatic Row Height for Wrapped Text

When you format a cell so that the information within it can wrap to multiple lines, you may be surprised if Excel ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

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

Kanishka

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


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.