Home |
Search |
Today's Posts |
#4
![]()
Posted to microsoft.public.excel.programming
|
|||
|
|||
![]()
If the other event you are referring to was a SelectionChange event then
in some cases you could use target and activecell interchangebly but this is not recommended. You haven't said exactly what it is that is not working with your new code and I still don't follow exactly what it is you want to have it do but for starters you have not properly qualified all the references in you With...End With block. If you put the statement Option Explicit right at the top of you module and try to run the event (change a cell on the sheet) you will be promted to declare the variable MergeCells. Because you have not qualified it it is being treated as a variable and hence having no effect. I have reworked this a bit so that it runs but it doesn't seem to make any real change to the format of the sheet. You may also want to see this reference to a post by Jim Rech on autofit with mergecells http://tinyurl.com/aknxy (thanks Norman). Reworked code: Private Sub Worksheet_Change(ByVal Target As Range) Dim CurrentRowHeight As Single, MergedCellRgWidth As Single Dim CurrCell As Range Dim TargetWidth As Single, PossNewRowHeight As Single On Error GoTo Exit_Event Application.EnableEvents = False If Target.MergeCells Then With Target.MergeArea .Select If .Rows.Count = 1 And .WrapText = True Then Application.ScreenUpdating = False CurrentRowHeight = .RowHeight TargetWidth = Target.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 = IIf(CurrentRowHeight PossNewRowHeight, _ CurrentRowHeight, PossNewRowHeight) End If End With End If Exit_Event: Application.EnableEvents = True End Sub Regards Rowan retseort wrote: Thanks I made the changes you suggested. Thanks for the Target versus Active Cell although I find it odd that it worked under another event. But you know this far better than I. However, I still cannot get this to work right any other ideas or am I still missing the boat. My new code... Private Sub Worksheet_Change(ByVal Target As Range) Dim CurrentRowHeight As Single, MergedCellRgWidth As Single Dim CurrCell As Range Dim TargetWidth As Single, PossNewRowHeight As Single Application.EnableEvents = False If Target.MergeCells Then With Target.MergeArea If .Rows.Count = 1 And .WrapText = True Then Application.ScreenUpdating = False CurrentRowHeight = .RowHeight TargetWidth = Target.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 = IIf(CurrentRowHeight PossNewRowHeight, _ CurrentRowHeight, PossNewRowHeight) End If End With End If Application.EnableEvents = True End Sub |
Thread Tools | Search this Thread |
Display Modes | |
|
|
![]() |
||||
Thread | Forum | |||
need help creating a Worksheet_Change code | Excel Worksheet Functions | |||
Code WAY too slow... (worksheet_change event) | Excel Programming | |||
worksheet_change vs. calculate, and worksheet_change not running | Excel Programming | |||
worksheet_change vs. calculate, and worksheet_change not running | Excel Programming |