Autofit row height to display wrapped text

FrankB

New Member
Joined
Sep 3, 2002
Messages
31
Hi,

Some lines of the wrapped multiline text don't appear in the cell. Is there any way to autofit the row height of all rows in the worksheet to display the entire text? Doubleclicking below the row narrows the row to cut off text, only dragging each row down individually works. Any ideas?

Thanks,

Frank
 
Below is the hightlighted code:

'Loop thru merged cells
For i = 0 To UBound(a)
Range(a(i)).Select
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 'This is where the code breaks
.EntireRow.AutoFit
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight > PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
MergedCellRgWidth = 0
Next i

the error text is as follows:
Runtime Error 1004
"Unable to set the ColumnWidth Property Cof the range Class"

It only occurs when the value of MergedCellRgWidth is greater than 255.
 
Upvote 0

Excel Facts

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.
I'm hoping someone can help me with this autofit row height problem. The macro listed here is exactly what I have been searching for, but when I run it, it gives me a 'Run-time error 9: subscript out of range' error.

When debuging the code, it flags this section:
'Loop thru merged cells
For i = 0 To UBound(a)
Range(a(i)).Select

I'm guessing this is operator error but I don't know where I went wrong. I cut and pasted the code snipet into module 1 at the very end. The spreadsheet in question does have multiple modules and about 20+ sheets in it. I'm afraid I don't know VB well enough to figure out what the error is.

If anyone has any ideas on how to fix this, I'd really appreciate it. This script is just what this workbook needs to fix things up.

Many thanks in advance
CAB
 
Upvote 0
The code you copied just above your post is only the second half of the needed code. the code only works on the active sheet. If you will detail which cells on which sheets you want to do this to I will customize the code to your liking. If you have time I will annotate it so you can tell what it is doing.

Or, if you just want it to check the entire worksheet use this code from earlier in the thread:


Code:
Sub AutoFitMergedCellRowHeight() 
    Dim CurrentRowHeight As Single, MergedCellRgWidth As Single 
    Dim CurrCell As Range 
    Dim ActiveCellWidth As Single, PossNewRowHeight As Single 
    Dim StartCell As Range, c As Range, MergeRng As Range, Cell As Range 
    Dim a() As String, isect As Range, i 

    
'Take a note of current active cell 
Set StartCell = ActiveCell 

'Create an array of merged cell addresses that have wrapped text 
For Each c In ActiveSheet.UsedRange 
If c.MergeCells Then 
    With c.MergeArea 
    If .Rows.Count = 1 And .WrapText = True Then 
        If MergeRng Is Nothing Then 
            Set MergeRng = c.MergeArea 
            ReDim a(0) 
            a(0) = c.MergeArea.Address 
        Else 
        Set isect = Intersect(c, MergeRng) 
            If isect Is Nothing Then 
                Set MergeRng = Union(MergeRng, c.MergeArea) 
                ReDim Preserve a(UBound(a) + 1) 
                a(UBound(a)) = c.MergeArea.Address 
            End If 
        End If 
    End If 
    End With 
End If 
Next c 


Application.ScreenUpdating = False 

'Loop thru merged cells 
For i = 0 To UBound(a) 
Range(a(i)).Select 
      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 
      
      If MergedCellRgWidth > 255 Then 
            MergedCellRgWidth = 255 
      End If 
            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 
MergedCellRgWidth = 0 
Next i 

StartCell.Select 
Application.ScreenUpdating = True 

'Clean up 
Set CurrCell = Nothing 
Set StartCell = Nothing 
Set c = Nothing 
Set MergeRng = Nothing 
Set Cell = Nothing 

End Sub

Let me know if you have any problems :bow:
 
Upvote 0
Thanks chead5

Many thanks for the offer of help. I had copied the whole code for doing a full worksheet. Sorry, I should have mentioned that in the original post. I took your code snipet from your reply and pasted that in the VBA Module 1 page again just to make sure that I had done it correctly. It's still giving me the same error.

Yes, idealy (sp) I'd like to make this work for a full worksheet. e.g. just have the sheet as the active (currently viewing) sheet and run the macro to fix the row heights. I'm betting that I have done something wrong, but don't know what and where. I don't think its your code, I'm guessing it's me. I copied the entire code from your reply starting with:
Sub AutoFitMergedCellRowHeight()
and ending with:
End Sub

The macro shows up fine in the list of macros, but then bombs with the error i mentioned when I try to run it. When I click on the debug button, it flags the start of the section of code I mentioned, namely:
For i = 0 To UBound(a)
Attempting to continue or step through that section of code, gives me the
run-time error 9 that I mentioned.

I really appreciate your offer of help. This is not a critical issue, I can live with the problem for now. But if working together, we can solve it, that would be great too. Maybe if you did have a chance to comment the code (no rush or urgency) I would be better able to tell you exactly what is wrong?

Thanks again
cab
 
Upvote 0
I just sent you a private message withy my e-mail address, send me a copy of your sheet.
 
Upvote 0
Here is what I found after looking at the code in cab's worksheet. This is a copy of my e-mail back to him.

figured out the problem, unfortunately it's not user error, it's a code issue. This macro was designed to autofit the height of merged cells across multiple columns (left to right). All of your merged cells are merged across a range of rows (up and down). The reason for the error is as follows.

The macro scrolls through every cell in the used range and does a test that determines if the cell is merged (code line 13: If c.MergeCells Then). It then tests to see if the cell is merged across rows or columns (code line 15 : If .Rows.Count = 1 And .WrapText = True Then). If this test passes the macro adds that cell reference to an array formula. The place where you are getting the error is where the work actually starts. The reason you are getting the error message is because none of your merged cells pass that second test. In turn, the array variable never gets initialized.


I'll take a look at the code and see if I can modify it to deal with cells that are meged across a range of rows. If anyone else wants to take a crack at it. It would be cool to see a couple different solutiuons here. :hammer:
 
Upvote 0
This is a brilliant code. Thanks Parry from Auckland

Hi Cathyo, replace the above code with this version. Just select the sheet in question then run the macro and this should fix all merged cells in the sheet. Try on a copy of your book first in case there are problems.

Code:
Sub AutoFitMergedCellRowHeight()
    Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
    Dim CurrCell As Range
    Dim ActiveCellWidth As Single, PossNewRowHeight As Single
    Dim StartCell As Range, c As Range, MergeRng As Range, Cell As Range
    Dim a() As String, isect As Range, i
 
 
'Take a note of current active cell
Set StartCell = ActiveCell
 
'Create an array of merged cell addresses that have wrapped text
For Each c In ActiveSheet.UsedRange
If c.MergeCells Then
    With c.MergeArea
    If .Rows.Count = 1 And .WrapText = True Then
        If MergeRng Is Nothing Then
            Set MergeRng = c.MergeArea
            ReDim a(0)
            a(0) = c.MergeArea.Address
        Else
        Set isect = Intersect(c, MergeRng)
            If isect Is Nothing Then
                Set MergeRng = Union(MergeRng, c.MergeArea)
                ReDim Preserve a(UBound(a) + 1)
                a(UBound(a)) = c.MergeArea.Address
            End If
        End If
    End If
    End With
End If
Next c
 
 
Application.ScreenUpdating = False
 
'Loop thru merged cells
For i = 0 To UBound(a)
Range(a(i)).Select
      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
MergedCellRgWidth = 0
Next i
 
StartCell.Select
Application.ScreenUpdating = True
 
'Clean up
Set CurrCell = Nothing
Set StartCell = Nothing
Set c = Nothing
Set MergeRng = Nothing
Set Cell = Nothing
 
End Sub
 
Upvote 0
Thank you for this excellent code.

I was tearing what little hair I have left out until I found this solution.

Peter from UK
 
Upvote 0
Hi parry,

I know this post was in July 2014, but want to give you a thumbs-up any way.

This helped me a lot today.:-D
 
Upvote 0

Forum statistics

Threads
1,224,060
Messages
6,176,145
Members
452,707
Latest member
laplajewelry

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top