View Single Post
  #4   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone Jim Cone is offline
external usenet poster
 
Posts: 3,290
Default Code looping until machine freezes!!?

Simon,

You can turn off the cascading events with...
Application.EnableEvents = False, but it must be turned
back on at the end of the code for Excel to function normally.
I made the following changes...
added the EnableEvents code
added leading dots necessary for the use of With
added a code line to remove the formatting if nothing in cell.
Also it seemed to make more sense to use the "Change" event
instead of the "Selection Change" event.

Regards,
Jim Cone
San Francisco, USA


"--------------
Private Sub Worksheet_Change(ByVal Target As Range)
On Error GoTo Err_Cleanup
Application.EnableEvents = False
Dim Mycell As Excel.Range
Dim rng As Excel.Range
Set rng = Range("V2:V40")
If Not Application.Intersect(Target(1), rng) Is Nothing Then
For Each Mycell In rng
If Len(Mycell) Then
With Mycell.Interior
.ColorIndex = 44
.Pattern = xlSolid
End With
End If
Next
End If
Err_Cleanup:
Application.EnableEvents = True
End Sub
'------------------


"Simon Lloyd"
wrote in message

Hi all,
I have tried to devise some code so that if a cell in a set range
contains a value or character of any kind to change to a colour, it
works but seems to loop many times until my machine freezes (i can
press escape to get out of it!) any ideas why and perhaps a nudge in
the right direction please!
Thanks,
Simon.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Dim mycell
Dim rng As Range

Set rng = Range("V2:V40")
For Each mycell In rng
If mycell < "" Then
mycell.Select
With Selection.Interior
ColorIndex = 44
Pattern = xlSolid
PatternColorIndex = xlAutomatic
End With
End If
Next
End Sub
Simon Lloyd