LinkBack Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 414
Default Worksheet_Change even code will not run

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
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
need help creating a Worksheet_Change code Cajun20th Excel Worksheet Functions 4 May 18th 06 09:12 AM
Code WAY too slow... (worksheet_change event) [email protected] Excel Programming 0 January 11th 05 08:34 PM
worksheet_change vs. calculate, and worksheet_change not running Tom Ogilvy Excel Programming 1 July 14th 03 02:51 AM
worksheet_change vs. calculate, and worksheet_change not running Ross[_5_] Excel Programming 0 July 13th 03 04:27 PM


All times are GMT +1. The time now is 05:23 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"