View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default Macro in 1 cell vs entire spreadsheet

left out the End for
for the NEXT

Sub AutoFitMergedCellRowHeight()
Dim cell As Range, rng As Range
Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
Dim CurrCell As Range
Dim ActiveCellWidth As Single, PossNewRowHeight As Single
For Each cell In Selection
If rng Is Nothing Then
Set rng = cell.MergeArea
Else
If Intersect(rng, cell.MergeArea) Is Nothing Then
If cell.MergeCells Then
Set rng = Union(rng, cell.MergeArea)
End If
End If
End If
Next
If rng Is Nothing Then Exit Sub
For Each cell In rng.Areas
cell.Select
If ActiveCell.MergeCells Then
With ActiveCell.MergeArea
If .Rows.Count = 1 And .WrapText = True Then
Application.ScreenUpdating = False
CurrentRowHeight = .RowHeight
ActiveCellWidth = ActiveCell.ColumnWidth
For Each CurrCell In Selection
MergedCellRgWidth = CurrCell.ColumnWidth + _
MergedCellRgWidth
Next
.MergeCells = False
.Cells(1).ColumnWidth = MergedCellRgWidth
.EntireRow.AutoFit
PossNewRowHeight = .RowHeight
.Cells(1).ColumnWidth = ActiveCellWidth
.MergeCells = True
.RowHeight = IIf(CurrentRowHeight PossNewRowHeight, _
CurrentRowHeight, PossNewRowHeight)
End If
End With
End If
Next
End Sub


"Darin Kramer" wrote in message
...
Thanks Tom,

I get a "For control variable already in use" error and it highlights
the following VBA

For Each cell In rng.Areas
(16 lines down)

This is the final Macro in the book I been preparing, so really
desparate for it to work. Thanks so much for the effort.



*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!