You’ve most likely heard this warning — “Avoid merged cells in your Excel worksheets!”, and that is excellent advice. Merged cells can cause problems, especially when they’re in a table that you’ll be sorting and filtering. You’ll run into more problems if you try to autofit merged cell row height.
Forced to Merge
Occasionally though, you might have no choice but to use one or more merged cells on a worksheet. As long as you avoid merging table cells, and proceed with caution, things might be okay.
In the example shown below, there is an order form, and space for a note about the order. If the note will always be short, there’s no need to merge the cells – just let the text flow across the columns.

However, if the notes will be two or more lines, you’ll need to merge the cells, and turn on Wrap Text. Adjusting the column width would affect the product list that starts in row 12, so that’s not an option.
Merged Cell Row Height
Usually, if you add more text to a single cell, and Wrap Text is turned on, the row height automatically adjusts, to fit the text.
When the cells are merged in row 10, the row height has to be manually adjusted when the text changes. That works well, as long as you remember to do it, but it can be a nuisance, if the text changes frequently.
And if you forget to adjust the row height, you might print the order form, while key instructions are hidden.

AutoFit Merged Cell Row Height
To fix the worksheet, so the merged cells adjust automatically, you can add event code to the worksheet.
[Update: The original code is below, and there are several modified versions of the code in the comments. There is also an updated version of Smallman’s code in this December 2015 blog post.]
The merged cells are named OrderNote, and that name will be referenced in the event code.

Code to AutoFit Merged Cell Row Height
We want the row height to adjust if the OrderNote range is changed, so we’ll add code to the Worksheet_Change event.
The code that I use is based on an old Excel newsgroup example, that was posted by Excel MVP, Jim Rech.
Note: As Jeff Weir pointed out in the comments below, this code will wipe out the Undo stack, so you won’t be able to undo any steps you’ve previously taken. So, instead of using the Worksheet_Change event, you could use the workbook’s BeforePrint event, to reduce the Undo problem.
- Right-click on the sheet tab, and paste the following code on the worksheet module. Note: Only one Worksheet_Change event is allowed in each worksheet module.
- Change the range name from “OrderNote”, to the named range on your worksheet.
- If your worksheet is protected, you can add code to unprotect and protect the worksheet.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim MergeWidth As Single
Dim cM As Range
Dim AutoFitRng As Range
Dim CWidth As Double
Dim NewRowHt As Double
Dim str01 As String
str01 = "OrderNote"
If Not Intersect(Target, Range(str01)) Is Nothing Then
Application.ScreenUpdating = False
On Error Resume Next
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.Count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
End Sub
How It Works
The event code checks to see if the changed cell is in the OrderNote range. If it is, the code runs, and does the following:
- Unmerge the cells
- Get the width of the first column in the OrderNote range
- Get the total width for all columns in the OrderNote range
- Add a little extra to the calculated width
- Set the first column to the calculated total width
- Autofit the row, based on the note next in the first column
- Get the new row height
- Change the first column to its original width
- Merge the cells
- Set the row height to the new height
Screen updating is turned off while the code runs, and it all happens in the blink of an eye.
Test the Event Code
To test the code, make a change to the text in the named merged cells, then press Enter. The row height should adjust automatically.
Is this code, to AutoFit merged cell row height, something that you’ll use in your workbooks? Please let me know in the comments.
__________________
Brilliant! Thanks Debra! I’ve been trying to figure this out for a couple months now! Thanks again!
First, I want to sincerely thank Debra and others who have provided this very helpful code. It resolved an issue I’ve had with a spreadsheet for some time. I am more of an analyst and not programmer, but find this process somewhat interesting and would like to know a little more about how this works.
I find that when I use this merged cell resizing code in a fairly complex worksheet (actually multiple sheets within a workbook) which includes password protection, that everytime I make a change to any cells in the worksheet, the ActiveSheet.Protect statement “re-protects” my sheet.
Is there an easy way to change the original code so that it will only resize with a change to the “OrderNote” cell? This would allow me to make changes to the template without it always going back to protected status.
Thanks
Chuck
Hi,
I used the code successfully on one merged cell but have 7 more on the same tab. I see it only works once per tab, but there was some discussion about applying to more than one and I couldn’t understand the posts completely (not technical at all!!. If there is one that has the code for this would you mind telling me the date and name of person who posted.
Thanks,
Hi Waveney
There are plenty of examples on this page of using this code with mulple cells. If you read the posts beneath you will see code for continuous and non continuous ranges of cells. Hope this helps.
Take care
Smallman
I am completely new with working in VBA and am having no luck getting this to work for me. I’m trying to create a workbook that creates a report (one worksheet formatted for printing) by pulling in information (via formulas) from other worksheets. The report is “generated” when a name is selected in a drop down menu, directed all the formulas to pull information based on that name. Part of the report is to populate merged cells with “notes” entered in a log from a different worksheet. Some of the notes are 3 or 4 lines long and would need to change the height of the row. I think I’m in the right place to automate this but I can’t seem to get it to work. In the notes section of the workbook, the rows go as followed (row 1) Date: (row 2) Type: (row 3) Notes: . Can someone give me a step by step instructions? Any help is greatly appreciated for this VBA beginner who loves Excel.
The whole comment didn’t post because I used brackets for part. The rows go as follows:
Row 1 – Date: (formula)
Row 2 – Type: (formula)
Row 3 – Notes: (formula in merged cells B3:G3) – this is the cells I want to auto adjust the height.
Thank you again.
Depending on your situation, this may actually be solvable without additional code. This is especially the case if you are delivering a printout or PDF instead of the Excel file itself.
Here’s an example:
Let’s say you’ve got a print area of columns A->E, and your merged cells are in columns C-E, with a combined width of 90.
All you have to do is set aside an unprinted column, width 90, with wordwrap on. For this example let’s say you can use column I. Then whenever you write a value to cell B#, write the same value to I#. The single cell in column I will then trigger the autoheight. And it will not show in printing. If your end user receives the excel file and you don’t want this second value to show, you can use a cell far to the right and set the font to white. Unlike charts, it won’t matter if your user finds the value – all they can do is damage the auto-height.