Macro VBA to Autofit row height of merged and un-merged/normal Cells

sewmai

New Member
Joined
Dec 27, 2017
Messages
1
Hi Everyone,
I have a worksheet (ranges A7 to F90) which contains a mix of merged and un-merged cells in each row.. What I would like to do is to create a button in which it will autofit the size of the rows based on the highest row size for both merged and un-merged... I have already prepared the script that will autofit the row size if it's a merged cell, however, I'm having a challenge when it comes to unmerged cells.. If the current row height is currently greater than the autofit height of unmerged cells (because of the merged cell autofit row height), I want to leave it as is... However, if it is less, then I want to use the autofit function to adjust to the correct row size..

Here's my working code so far but currently got stuck..

Code:
Sub TestForMergedCell_version2()Dim rCheckCell As Range
Dim rCheck As Range
Dim HeightChecker As Integer
On Error Resume Next
Application.ScreenUpdating = False 'Speed up code and stop screen flickering
Application.EnableEvents = False  'Also stops endless loops in Events
Application.DisplayAlerts = False


Set rCheck = ActiveWorkbook.ActiveSheet.Range("A7:F90")
Set rCheckCell = ActiveCell
For Each rCheckCell In rCheck.Cells
If rCheckCell.MergeCells = True Then
With rCheckCell
    .Select
.RowHeight = 1
.WrapText = True
    .UnMerge
    .Cells(1).EntireRow.AutoFit
    h = .Cells(1).RowHeight
    .Merge
    .EntireRow.AutoFit
    With .Cells(1).MergeArea
        .Cells(.Cells.Count).RowHeight = _
           .Cells(.Cells.Count).RowHeight + (h - .Height)
    Selection.Merge
    End With
End With
End If
If rCheckCell.MergeCells = False Then
With rCheckCell
HeightChecker = 30
If rCheckCell.RowHeight < HeightChecker Then rCheckCell.WrapText = True
End With
End If
Next
End Sub

nUNQFIK.png

lqGTC
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,214,606
Messages
6,120,485
Members
448,967
Latest member
visheshkotha

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