Auto Row Height containing merged cells

Hart

Board Regular
Joined
Jan 2, 2005
Messages
71
Hi board members:
I have a sheet with several hundred rows containing merged cells. I need to be able to adjust the row height to accommodate the text in merged cells (column B through column J) if column A contains the word "Comment". Any help would be greatly appreciated.
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Run the "Auto_Row_Height" macro on your sheet:

VBA Code:
Sub Auto_Row_Height()
  Dim i As Long
    Application.ScreenUpdating = False
    For i = 2 To Range("A" & Rows.Count).End(xlUp).Row
      If Cells(i, "A") = "Comment" Then
        Call Adjust_Row(Range("B" & i & ":J" & i))
      End If
    Next
    Application.ScreenUpdating = True
End Sub
'
Sub Adjust_Row(rngRango As Range)
  Dim n, sngAnchoTotal, sngAnchoCelda, sngAlto
    For n = 1 To rngRango.Columns.Count
        sngAnchoTotal = sngAnchoTotal + rngRango.Cells(1, n).ColumnWidth
    Next n
    With rngRango.Cells(1, 1)
        sngAnchoCelda = .ColumnWidth
        .MergeCells = False
        .ColumnWidth = sngAnchoTotal
        .WrapText = True
        rngRango.Parent.Rows(rngRango.Row).AutoFit
        sngAlto = .RowHeight
    End With
    With rngRango
        .Merge
        .HorizontalAlignment = xlGeneral
        .VerticalAlignment = xlTop
        .Columns(1).EntireColumn.ColumnWidth = sngAnchoCelda
        .Columns(1).RowHeight = sngAlto
    End With
End Sub
 
Upvote 0
Hi Dante and thanks so much! This works perfectly with the exception that cell A1 need only contain the word "Comment" as opposed to ="Comment". "Comment" will always be the last seven characters in cell A1.
 
Upvote 0
Change this line:

If Right(Cells(i, "A"), 7) = "Comment" Then
 
Upvote 0
The vba code works fine on my version - 2016 but not on 2010. Any suggestions? There is an extra line added to the line height and other cells have been widened - not sure what to think.
 
Upvote 0

Forum statistics

Threads
1,215,720
Messages
6,126,443
Members
449,314
Latest member
MrSabo83

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