VBA--Autofit rows with merged cells

BradA

Board Regular
Joined
Sep 24, 2010
Messages
75
I have designed a report contains VBA code (call it the "Filler code") that allows users to fill the report with data that comes from several other spreadsheets. Other people in the company had set the report format up and I'm more or less stuck with the report template that they have developed. Unfortunately it has a lot of merged cells. A quick Google search gave me the following code to adjust row height on merged cells:

Code:
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

It works excellently. But it seems to only work when we're only merging columns and not rows. Is there a way to do this when we're merging rows?

I have the above code placed in a regular module but call it from the Worksheet Change event. I was expecting to see the height adjustment code run each time that the Filler code placed data into a cell on the report. Is there a way that I can get the height adjustment code to run each time that the Filler code inputs data? Or is there a better way to do this, such as to go through each cell in the report once I'm done and have it auto adjust at that time?

Thanks in advance and sorry for having such a muddled question/explanation.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).

Forum statistics

Threads
1,226,508
Messages
6,191,444
Members
453,658
Latest member
healmo

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