<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...
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
-----------------------------
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:
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
--
Dave Peterson
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.
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
Right-clcik on the sheet tab and "View Code"
Copy/paste the code into that sheet module.
Gord Dibben MS Excel MVP
I followed the directions but the code doesn't run on text entry. Can you
tell me what I'm doing wrong?
Jacki
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:
Thanks.
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
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
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
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