Hi all,
I'm trying to auto fit rows that are merged. I have tried this code:
Private Sub Worksheet_Change2(Optional ByVal Target As Range)
Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
Dim CurrCell As Range
Dim TargetWidth As Single, PossNewRowHeight As Single
Dim a As Range
If Target.MergeCells Then
Set a = Cells(Target.Row, Target.Column)
If a.Value <> "" Then
With a.MergeArea
If .Rows.Count = 1 And .WrapText = True Then
Application.ScreenUpdating = False
CurrentRowHeight = .RowHeight
TargetWidth = a.ColumnWidth
For Each CurrCell In Selection
MergedCellRgWidth = CurrCell.ColumnWidth + _
MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = TargetWidth
.MergeCells = True
.RowHeight = PossNewRowHeight
End If
End With
End If
If a = "" Then
a.RowHeight = 15
End If
End If
End Sub
But end up with a "Run-time 91" error. "Object variable or With Block variable not set." The debug then highlights "If Target.MergeCells Then".
I'm new to VBA and am unsure how to personalise this VBA (it was written for someone else).
Thank you in advance
Bookmarks