Hi guys,
I need some help auto fitting text in some merged cells.
I got the following code, thanks to Debra Dalgleish (http://blog.contextures.com/archives...ll-row-height/), that works great for a single named range. But, the problem is that I have multiple named ranges I would like to auto fit on the same sheet.
Is there perhaps a way i can do this? perhaps a loop or something? from either one range name to the next, or loop from row to row?
My ranges are named DelayDetail; DelayDetail2; DelayDetail3 etc.
Private Sub Worksheet_Change(ByVal Target As Range)
Dim MergeWidth As Single
Dim cM As Range
Dim AutoFitRng As Range
Dim CWidth As Double
Dim NewRowHt As Double
Dim str01 As String
str01 = "DelayDetail"
If Not Intersect(Target, Range(str01)) Is Nothing Then
Application.ScreenUpdating = False
On Error Resume Next
Set AutoFitRng = Range(Range(str01).MergeArea.Address)
With AutoFitRng
.MergeCells = False
CWidth = .Cells(1).ColumnWidth
MergeWidth = 0
For Each cM In AutoFitRng
cM.WrapText = True
MergeWidth = cM.ColumnWidth + MergeWidth
Next
'small adjustment to temporary width
MergeWidth = MergeWidth + AutoFitRng.Cells.count * 0.66
.Cells(1).ColumnWidth = MergeWidth
.EntireRow.AutoFit
NewRowHt = .RowHeight
.Cells(1).ColumnWidth = CWidth
.MergeCells = True
.RowHeight = NewRowHt
End With
Application.ScreenUpdating = True
End If
End Sub
Thank you!
Bookmarks