Issue w/ Autofit Row Heights
Pete, thank you for the kind words. However, as I pointed out, the code is an
adaption of mine of an old Jim Rech post that I saw years past and HE is the
author of the "brilliant" part of it. I adapted it to be event driven only
and shortened the variable names to suit my style.
Greg
"Pete at Sappi Fine Paper" wrote:
Greg, this is brilliant! I have a spreadsheet which requires a merged cell
to autofit wrapped text, and your code works a treat! Thanks so much for
this.
Cheers,
Pete
"Greg Wilson" wrote:
The code you posted was originally developed by Jim Rech. This is an adaption
of his code from a recent post of mine. Paste to the worksheet's code module.
To do so, right click the worksheet's tab and select View Code to access the
code module. Also, ensure that the WrapText property of the merged cells is
set to True through Format Cells Alignment tab.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim NewRwHt As Single
Dim cWdth As Single, MrgeWdth As Single
Dim c As Range, cc As Range
Dim ma As Range
With Target
If .MergeCells And .WrapText Then
Set c = Target.Cells(1, 1)
cWdth = c.ColumnWidth
Set ma = c.MergeArea
For Each cc In ma.Cells
MrgeWdth = MrgeWdth + cc.ColumnWidth
Next
Application.ScreenUpdating = False
ma.MergeCells = False
c.ColumnWidth = MrgeWdth
c.EntireRow.AutoFit
NewRwHt = c.RowHeight
c.ColumnWidth = cWdth
ma.MergeCells = True
ma.RowHeight = NewRwHt
cWdth = 0: MrgeWdth = 0
Application.ScreenUpdating = True
End If
End With
End Sub
Regards,
Greg
|