newbie VBA help please
Words of warning.
First, if you are playing with VBA, you DO know enough to be dangerous <vbg
Second, if you can, avoid merged cells. They cause more problems than they
are worth. You can usually design around it.
--
HTH
Bob Phillips
"SteveFerd" wrote in message
...
Hello, Thanks in advance for your help.
Before I even ask my question, I am far from being a programer and don't
really even know enough to be dangerous. I am developing a template for a
group of 40 sales reps which will be used to track large accounts. The
template requires the use of merged cells which will need to be able to
use
word wrap. I followed previous advice and use the following code from Jim
Rech:
''Simulates row height autofit for a merged cell if the active cell..
'' is merged.
'' has Wrap Text set.
'' includes only 1 row.
''Unlike real autosizing the macro only increases row height
'' (if needed). It does not reduce row height because another
'' merged cell on the same row may needed a greater height
'' than the active cell.
Sub AutoFitMergedCellRowHeight()
Dim CurrentRowHeight As Single, MergedCellRgWidth As Single
Dim CurrCell As Range
Dim ActiveCellWidth As Single, PossNewRowHeight As Single
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
End Sub
This works fine, just need it to be automatic. So I added this to the
worksheet:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
AutoFitMergedCellRowHeight
End Sub
This mostly works. When I enter text in a merged cell which will require
wrap then press enter, it does not automatically wrap. But when I go back
and click on the cell, it wraps. What do I need to do so I don't have to
go
back and click on the cell?
Thanks,
Steve
|