PC Review


Reply
Thread Tools Rate Thread

Calculating Number of Rows for Merged Cell Text Wrap

 
 
George Lee
Guest
Posts: n/a
 
      27th Jun 2008
I have lengthy string that I want to display in entirety. I would like to
pick how many cells wide to merge and based on that, have Excel calculate for
me how many rows I need. How is this done?
 
Reply With Quote
 
 
 
 
JLGWhiz
Guest
Posts: n/a
 
      27th Jun 2008
You can specify the width of the column, set it to wordwrap then use autofit
and it will put the correct number of rows.

"George Lee" wrote:

> I have lengthy string that I want to display in entirety. I would like to
> pick how many cells wide to merge and based on that, have Excel calculate for
> me how many rows I need. How is this done?

 
Reply With Quote
 
JLGWhiz
Guest
Posts: n/a
 
      27th Jun 2008
Here is some more info from Jim Rech about merged cells and autofit.

Unfortunately autofit ignores merged cells. The only workaround I know of
is a macro that simulates autofit for row heights. Of course it's not
automatic unless you call it from the worksheet change event.

''Simulates row height autofit for a merged cell if the active cell..
'' is merged.
'' has Wrap Text set.
'' includes only 1 row.
''Unlike real autosizing the macro only increases row height
'' (if needed). It does not reduce row height because another
'' merged cell on the same row may needed a greater height
'' than the active cell.
Sub AutoFitMergedCellRowHeight()
Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
Dim CurrCell As Range
Dim ActiveCellWidth As Single, PossNewRowHeight As Single
If ActiveCell.MergeCells Then
With ActiveCell.MergeArea
If .Rows.Count = 1 And .WrapText = True Then
Application.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth = ActiveCell.ColumnWidth
For Each CurrCell In Selection
MergedCellRgWidth = CurrCell.ColumnWidth +
MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End If
End Sub


--
Jim Rech
Excel MVP




"George Lee" wrote:

> I have lengthy string that I want to display in entirety. I would like to
> pick how many cells wide to merge and based on that, have Excel calculate for
> me how many rows I need. How is this done?

 
Reply With Quote
 
George Lee
Guest
Posts: n/a
 
      1st Jul 2008
I found that page and have used it. Quite good. This was one of those
functions that seems it should have been atomic to Excel. Thanks.

"JLGWhiz" wrote:

> Here is some more info from Jim Rech about merged cells and autofit.
>
> Unfortunately autofit ignores merged cells. The only workaround I know of
> is a macro that simulates autofit for row heights. Of course it's not
> automatic unless you call it from the worksheet change event.
>
> ''Simulates row height autofit for a merged cell if the active cell..
> '' is merged.
> '' has Wrap Text set.
> '' includes only 1 row.
> ''Unlike real autosizing the macro only increases row height
> '' (if needed). It does not reduce row height because another
> '' merged cell on the same row may needed a greater height
> '' than the active cell.
> Sub AutoFitMergedCellRowHeight()
> Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
> Dim CurrCell As Range
> Dim ActiveCellWidth As Single, PossNewRowHeight As Single
> If ActiveCell.MergeCells Then
> With ActiveCell.MergeArea
> If .Rows.Count = 1 And .WrapText = True Then
> Application.ScreenUpdating = False
> CurrentRowHeight = .RowHeight
> ActiveCellWidth = ActiveCell.ColumnWidth
> For Each CurrCell In Selection
> MergedCellRgWidth = CurrCell.ColumnWidth +
> MergedCellRgWidth
> Next
> .MergeCells = False
> .Cells(1).ColumnWidth = MergedCellRgWidth
> .EntireRow.AutoFit
> PossNewRowHeight = .RowHeight
> .Cells(1).ColumnWidth = ActiveCellWidth
> .MergeCells = True
> .RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
> CurrentRowHeight, PossNewRowHeight)
> End If
> End With
> End If
> End Sub
>
>
> --
> Jim Rech
> Excel MVP
>
>
>
>
> "George Lee" wrote:
>
> > I have lengthy string that I want to display in entirety. I would like to
> > pick how many cells wide to merge and based on that, have Excel calculate for
> > me how many rows I need. How is this done?

 
Reply With Quote
 
Josh
Guest
Posts: n/a
 
      5th Aug 2008
This works well for autofitting with content. But if I clear the content of
the cell, the row does not shrink to fit the empty cell (in other words,
revert to the default row height).

Is there code you can provide to do this?

Thanks in advance.


"JLGWhiz" wrote:

> Here is some more info from Jim Rech about merged cells and autofit.
>
> Unfortunately autofit ignores merged cells. The only workaround I know of
> is a macro that simulates autofit for row heights. Of course it's not
> automatic unless you call it from the worksheet change event.
>
> ''Simulates row height autofit for a merged cell if the active cell..
> '' is merged.
> '' has Wrap Text set.
> '' includes only 1 row.
> ''Unlike real autosizing the macro only increases row height
> '' (if needed). It does not reduce row height because another
> '' merged cell on the same row may needed a greater height
> '' than the active cell.
> Sub AutoFitMergedCellRowHeight()
> Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
> Dim CurrCell As Range
> Dim ActiveCellWidth As Single, PossNewRowHeight As Single
> If ActiveCell.MergeCells Then
> With ActiveCell.MergeArea
> If .Rows.Count = 1 And .WrapText = True Then
> Application.ScreenUpdating = False
> CurrentRowHeight = .RowHeight
> ActiveCellWidth = ActiveCell.ColumnWidth
> For Each CurrCell In Selection
> MergedCellRgWidth = CurrCell.ColumnWidth +
> MergedCellRgWidth
> Next
> .MergeCells = False
> .Cells(1).ColumnWidth = MergedCellRgWidth
> .EntireRow.AutoFit
> PossNewRowHeight = .RowHeight
> .Cells(1).ColumnWidth = ActiveCellWidth
> .MergeCells = True
> .RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
> CurrentRowHeight, PossNewRowHeight)
> End If
> End With
> End If
> End Sub
>
>
> --
> Jim Rech
> Excel MVP
>
>
>
>
> "George Lee" wrote:
>
> > I have lengthy string that I want to display in entirety. I would like to
> > pick how many cells wide to merge and based on that, have Excel calculate for
> > me how many rows I need. How is this done?

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Merged cell text does not wrap Jessica Microsoft Excel Misc 1 5th Dec 2007 03:00 AM
rows of text in merged cell with wrap text =?Utf-8?B?bWFyaw==?= Microsoft Excel Programming 1 19th May 2006 06:39 AM
Wrap Text in Merged Cell stefan via OfficeKB.com Microsoft Excel Programming 7 10th Jul 2005 07:38 PM
Rows with merged cells are not adjusting even w/ Wrap Text and au. =?Utf-8?B?bWtlcm4yMA==?= Microsoft Excel Misc 1 4th Jan 2005 08:10 PM
How to wrap text in a merged cell kristi Microsoft Excel Misc 2 28th Jan 2004 06:21 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:52 AM.