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..
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