Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Office 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

by Allen Wyatt
(last updated May 23, 2020)

13

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 Office 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

Transposing Two Paragraphs

Need to swap two adjacent paragraphs? Your editing arsenal can include a command to do this is you use the macro in this tip.

Discover More

Searching for Special Hyphens

Word allows you to use a couple of different types of hyphens in your document, each with a different purpose. If you ...

Discover More

Finding the Address of the Lowest Value in a Range

Uncovering the lowest value in a range is relatively easy; you can just use the MIN worksheet function. Discovering the ...

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 for Your Text

Want Excel to automatically adjust the height of a worksheet row when it wraps text within the cell? It's easy to do, ...

Discover More

Increasing Row Height for Printing

You may have a need to increase the height of the rows in your worksheet to "spread out" the data when it is printed. ...

Discover More

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
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 six more than 1?

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.