Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Merged cells won't Autofit row height

5,245 views
Skip to first unread message

Hpyifur

unread,
Jan 12, 2007, 12:10:01 PM1/12/07
to
When I merge cells and then choose the wrap text option the Autofit function
no longer works. Is there a way to get around this without manually sizing
the row each time?

RagDyer

unread,
Jan 12, 2007, 12:24:45 PM1/12/07
to
*Unmerge* the cells and then use "Center Across Selection" from:

<Format> <Cells. <Alignment> tab,
Expand the "Horizontal" window and choose "Center Across Selection".
--
HTH,

RD

---------------------------------------------------------------------------
Please keep all correspondence within the NewsGroup, so all may benefit !
---------------------------------------------------------------------------
"Hpyifur" <Hpy...@discussions.microsoft.com> wrote in message
news:0518FECC-94CC-40B2...@microsoft.com...

Hpyifur

unread,
Jan 12, 2007, 12:42:00 PM1/12/07
to
RD - Thanks for your response.

My problem is that it's an area that needs to have information entered into
by other people (and could vary greatly as to the input). It has to be left
justified and is limited on how wide the merged area can be, that's why I was
looking to use the Wrap Text option.

Thanks
-----------------------------

Gord Dibben

unread,
Jan 12, 2007, 1:38:12 PM1/12/07
to
Long audible sigh here.................

One more victim of "merged cells".

Wrap Text works fine on merged cells, but Autofit does not work.

You need VBA code to do that.

Here is code from Greg Wilson.

Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewRwHt As Single
Dim cWdth As Single, MrgeWdth As Single
Dim c As Range, cc As Range
Dim ma As Range

With Target
If .MergeCells And .WrapText Then
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
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
Application.ScreenUpdating = True
End If
End With
End Sub


Gord Dibben MS Excel MVP

On Fri, 12 Jan 2007 09:42:00 -0800, Hpyifur <Hpy...@discussions.microsoft.com>
wrote:

Chris

unread,
Mar 5, 2007, 1:20:05 AM3/5/07
to
Gord
I to used you code, works great, however not if I protected document. Any
suggestions?
--
Regards

Greg Wilson

unread,
Mar 5, 2007, 2:16:09 AM3/5/07
to
In place of the word "password" you would hard code your password and remove
the leading apostrophe assuming it's password protected. Otherwise ignore it.
Minimal testing:-

Private Sub Worksheet_Change(ByVal Target As Range)
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
End Sub

Regards,
Greg

Odie

unread,
Jun 28, 2007, 11:02:04 AM6/28/07
to
Is there any solution for this problem without the use of code?

Dave Peterson

unread,
Jun 28, 2007, 11:26:48 AM6/28/07
to
Adjust the rowheight manually????

--

Dave Peterson

Odie

unread,
Jun 28, 2007, 12:02:02 PM6/28/07
to
Dave, thanks. I guess I should have been more specific.

Is there anyway to make the autofit work with merged cells (other than using
code)? I have a form that is used over and over, so it's a pain to keep
manually adjusting (plus the screen view is different than print view, so it
means lots of switching back and forth).

Another work around that doesn't solve the underlying problem is to redesign
the form.

Gord Dibben

unread,
Jun 28, 2007, 12:53:46 PM6/28/07
to
You were quite specific as was Dave's answer.

Without code you must manually adjust the heights.

Blow away those merged cells and forget that feature exists to make life much
simpler.


Gord Dibben MS Excel MVP

code in Excel@discussions.microsoft.com Using code in Excel

unread,
Oct 1, 2007, 6:07:01 PM10/1/07
to
How is this code actually used?

Gord Dibben

unread,
Oct 1, 2007, 7:49:02 PM10/1/07
to
The code is event code and runs upon entry of text to merged cells.

Right-clcik on the sheet tab and "View Code"

Copy/paste the code into that sheet module.


Gord Dibben MS Excel MVP

Aquarianrep

unread,
Nov 3, 2007, 6:54:01 PM11/3/07
to
This is working perfectly for me, however, I will need to send the
spreadsheets I created to customers. I'd like to get the macro certificate
number, but I don't know how. Any advice?

@discussions.microsoft.com Jacki

unread,
Nov 10, 2007, 11:46:02 AM11/10/07
to
Gord

I followed the directions but the code doesn't run on text entry. Can you
tell me what I'm doing wrong?

Jacki

Gord Dibben

unread,
Nov 10, 2007, 1:46:23 PM11/10/07
to
From here........no.

Did you copy/paste the code into the appropriate worksheet module?

Do you have "wrap text" enabled on these merged cells?

Maybe events have been disabled.

Run this macro to enable events then use the revised code below to make sure
they get re-enabled on error.

Sub enable_events()
Application.EnableEvents = True
'or just paste the one line to the Immediate window and hit ENTER
End Sub


Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewRwHt As Single
Dim cWdth As Single, MrgeWdth As Single
Dim c As Range, cc As Range
Dim ma As Range
With Target
If .MergeCells And .WrapText Then

On Error GoTo endit
Application.EnableEvents = False


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
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
Application.ScreenUpdating = True
End If
End With

endit:
Application.EnableEvents = True
End Sub


Gord

On Sat, 10 Nov 2007 08:46:02 -0800, Jacki <Jacki @discussions.microsoft.com>
wrote:

PennStan

unread,
Nov 15, 2007, 10:07:01 PM11/15/07
to
Just ran into this problem today. Never thought autoheight would not work
for merged cells since it works for single unmerged cell. Glad I found the
solution here. Macro works fine.

nanook

unread,
Jan 21, 2008, 10:15:01 AM1/21/08
to
Hi Greg,
I used your code and it worked perfectly...except:
If someone writes text in a cell (which is long enough to wrap and the
height to be adjusted) and then clicks either tab or enter to move to another
cell, a pop-up appears asking for a password in order to unprotect sheet. If
the user then presses escape, without entering a password, and then
tools-protection-unprotect sheet it doesn't ask for the password and just
unprotects!!
I think I must have missed something in the code!
Thanks

nanook

unread,
Jan 21, 2008, 12:27:01 PM1/21/08
to
Sorry, I've just spotted my error - I hadn't removed the leading apostrophe
as you said was needed!

Lynn

unread,
Apr 4, 2008, 3:13:04 PM4/4/08
to
Way cool!! Thanks!!
--
-Lynn F. PMP

TimS

unread,
Jun 18, 2008, 9:01:00 PM6/18/08
to
Hi, Have loaded this code into the VB area of Excel, but it doesn't run.
Expect that I am missing something, can you please advise how to load it.

Thanks.

TimS

unread,
Jun 18, 2008, 9:18:01 PM6/18/08
to
Just worked out how to do it by right-clicking on sheet tab and "View Code".

mooresk257

unread,
May 3, 2009, 6:16:01 PM5/3/09
to
In Excel 2007, this code also changes the cell property from "unlocked" to
"locked". I think I'll see how I can restructure my sheet to avoid merging
cells.

Jeno

unread,
Jul 20, 2009, 5:43:01 PM7/20/09
to
The cell I use the code in locks after i enter text and go to the next cell;
I'm unable to access cell much less re-enter text after I type once and go to
another cell. I'm using excel 2007 and don't use a password when protecting
sheet. After I click "Protect Sheet", click ok without entering password,
enter text into cell, I'm prompted with something about a expanind width, I
click "yes" again, but when I try to go back to cell I can't because it
become locked. How can I protect sheet, use "text wrap in merge cell" code,
enter data, expand merged cells accordingly, not have the cell lock after I
enter text, and be able to re-enter data whenever?

ELizzy010

unread,
Sep 17, 2009, 2:23:16 PM9/17/09
to
I am new to Visual Basic.

I pasted the code into the box that appeared when I selected "View Code."
However, when I returned to the Worksheet I still could not autofit (either
by double-clicking the row or by selecting "Autofit").

How may I activate the code?

I tried (1) "Save" and (2) "Run" ... which created a Macro that runs when I
open the document but I cannot locate the Macro to use / delete it.

The Procedure is currently set to "Change." I wondered if that was a
problem since this is "Event" activated ...

Thank you for your patience!
Erin

Gord Dibben

unread,
Sep 17, 2009, 5:23:44 PM9/17/09
to
When you right-click on the sheet tab and "View Code" a sheet module(not a
"box") will open.

Paste the code into that module.

The rows must be preset to WrapText and Autofit.

You do not "run" the code nor do you double-click on a row

The "change" event takes place when you type something into a merged cell
and hit the ENTER key.

At that point the code is "activated" and your row expands to fit.


Gord

Tommy-ID

unread,
Jan 11, 2010, 11:38:01 PM1/11/10
to
Dear Gord Dibben,

Looks very helpfull thanks. Anyway, seems like it only work with merged
cells that merged the on one row (e.g. A1:A2). I try to merge A1:B2 or
multiple row and the error shows: Unable to set the ColumnWidth property of
the Range class. Is there any way to solve this?

Thanks.

Tommy-ID

Gord Dibben

unread,
Jan 12, 2010, 5:01:53 PM1/12/10
to
The configuation of the merged cells is not your problem.

Works for me with A1:B2 merged.......also A5:D9

Wrap text and row autofit have to be pre-set.

Back to your error message.................

You will receive that particular error message when the worksheet is
protected.

Maybe you want to unprotect then re-protect?

Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewRwHt As Single
Dim cWdth As Single, MrgeWdth As Single
Dim c As Range, cc As Range
Dim ma As Range

With Target
If .MergeCells And .WrapText Then

On Error GoTo endall
Me.Unprotect Password:="justme"


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
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
Application.ScreenUpdating = True
End If
End With

endall:
Me.Protect Password:="justme"
End Sub


Gord

pat...@gmail.com

unread,
Nov 9, 2013, 2:25:08 PM11/9/13
to
Gord, it has been a while since you wrote this, but I'm going to shoot this out into the ether in the hopes that it will reach you. Is there any way to make this so that it doesn't empty the undo cache? I understand that the workbook beforeprint event does something like what I'm looking for, but don't know how to change your code so that it works there.

Thank you in advance,
Pat

rdis...@googlemail.com

unread,
Mar 28, 2014, 8:03:49 PM3/28/14
to
Thanks so much for this! Save me on a friday afternoon.

amar...@arvada.org

unread,
Jun 9, 2017, 4:01:16 PM6/9/17
to
On Friday, January 12, 2007 at 10:10:01 AM UTC-7, Hpyifur wrote:
> When I merge cells and then choose the wrap text option the Autofit function
> no longer works. Is there a way to get around this without manually sizing
> the row each time?

I know this isn't an answer to your question but I'm looking for an answer as to how to autofit row height in Google Sheets and can't find it ANYWHERE! How do you do that? I can't find it in menus. It's simple in Excel. Thanks, and hope you got an answer to your question!

fran...@results4america.org

unread,
Feb 2, 2018, 10:39:24 AM2/2/18
to
Hi! I know this is an old forum, but did you ever find the code? I'm also looking for it! Thank you!
0 new messages