View Single Post
  #12   Report Post  
Posted to microsoft.public.excel.misc
Using code in Excel Using code in Excel is offline
external usenet poster
Posts: 1
Default Merged cells won't Autofit row height

How is this code actually used?

"Gord Dibben" wrote:

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
Application.ScreenUpdating = False
ma.MergeCells = False
c.ColumnWidth = MrgeWdth
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

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.


"RagDyer" wrote:

*Unmerge* the cells and then use "Center Across Selection" from:

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


Please keep all correspondence within the NewsGroup, so all may benefit !
"Hpyifur" wrote in message
When I merge cells and then choose the wrap text option the Autofit
no longer works. Is there a way to get around this without manually
the row each time?