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
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Hi Chead5, thank you for taking the time to advise issues with the code. Could you advise what the error is and which line is highlighted when you click debug. There may be other ways to resolve the issue. :)
Hi all,

Am getting the below error can you please advise how to fix this

VBA 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
 

Attachments

  • ACapture.JPG
    ACapture.JPG
    106.8 KB · Views: 4
Upvote 0
Unless I miss my guess your error is occuring on this line.
For i = 0 To UBound(a)

The code seems to deal with a very specific issue and I think you would be better off starting a separate thread for your own issue.
For this code to work you need to have at least 2 places in your spreadsheet that have the columns merged but not the rows and the area needs to have wrap turned on.
I suspect that this is not the case for you.
 
Upvote 0
Thanks for the reply, actually I am looking for the script which should apply auto-fit with merged cells.

Please refer screenshot attached, am expecting the output should be auto-fit when I run this VBA.

It’s worked well sometimes but I often face this error in the For i = 0 To UBound(a) can you please help on this?
 

Attachments

  • Picture1.png
    Picture1.png
    84.7 KB · Views: 5
  • Output.png
    Output.png
    108.9 KB · Views: 5
Upvote 0
I often face this error in the For i = 0 To UBound(a) can you please help on this?
When you experience the error, check that the sheet that is active has merged cells meeting the criteria below.
  • The cells must be merged across columns
  • The merged cells must all be in the same row (not merging rows)
  • The merged cell must be set to Wrap text
  • There must be at least 2 sets of merged cells.
The workbook in your screenshot appears to meet those criteria.

The key criteria are driven by this line in the code:
If .Rows.Count = 1 And .WrapText = True Then
 
Upvote 0
When you experience the error, check that the sheet that is active has merged cells meeting the criteria below.
  • The cells must be merged across columns
  • The merged cells must all be in the same row (not merging rows)
  • The merged cell must be set to Wrap text
  • There must be at least 2 sets of merged cells.
The workbook in your screenshot appears to meet those criteria.

The key criteria are driven by this line in the code:
If .Rows.Count = 1 And .WrapText = True Then
Thank you so much Alex for the explanation.



You’re really Mr.excel I simply removed (.WrapText = True) VBA code working fine.



Thank you again for the support😊
 
Upvote 0

Forum statistics

Threads
1,224,061
Messages
6,176,150
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