Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Activecell change 2

Status
Not open for further replies.

Clarkie001

IS-IT--Management
Aug 26, 2003
59
GB
Hi,

I have the following code which works fine for what I want, but when I select another cell within the range of C1 to C8 I need the previous cells Interior to change back to xlnone.

Can anyone shed any light on this?....Thanks in advance.

Clarkie

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

If ActiveCell >= Range(&quot;C1&quot;) And ActiveCell <= Range(&quot;C8&quot;) Then

With ActiveCell.Interior
.ColorIndex = 41
.Pattern = xlSolid
End With

Range(&quot;A1&quot;).Value = ActiveCell
Else
DoEvents
End If

End Sub
 
Just use TARGET = any range of your choice and you can call any macro depending on the value of TARGET at that point in your program

Richard
 
This should help you out:
Code:
Option Explicit
Public PrevTarget As Range

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

On Error Resume Next

If ActiveCell.Column = 3 And ActiveCell.Row >= 1 And _
    ActiveCell.Row <= 8 Then
    
    If PrevTarget.Column = 3 And PrevTarget.Row >= 1 And _
    PrevTarget.Row <= 8 Then
        PrevTarget.Interior.ColorIndex = xlNone
    End If
    
    Target.Interior.ColorIndex = 41
    
Range(&quot;A1&quot;).Value = ActiveCell.Address
Else
DoEvents
End If

Set PrevTarget = Nothing
Set PrevTarget = ActiveCell

End Sub

I created a public variable &quot;PrevTarget&quot; that will allow Excel to &quot;remember&quot; the previous cell that was selected. You might want to tweek it a bit to get it to do EXACTLY what you want ;-)

Enjoy!



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
tbl makes a good point, but I tend to use the Public variable just in case I want to use Target for something else later within the event.



Peace! [peace]

Mike

Never say Never!!!
Nothing is impossible!!!
 
The last code is not foolproof as it doesn't work going from bottom to top.This seems to work in all circumstances.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
Set CurrentCell = ActiveCell
Set isect = Application.Intersect(Range(ActiveCell.Address), Range(&quot;C1:C8&quot;))
If Not isect Is Nothing Then
Range(&quot;C1:C7&quot;).Interior.ColorIndex = xlNone
With CurrentCell.Interior
.ColorIndex = 41
.Pattern = xlSolid
End With
Range(&quot;A1&quot;).Value = ActiveCell.Address
Else
DoEvents
End If

End Sub
 
I know this seems to have been sorted but you could also use the sledgehammer approach:-

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
If Not Intersect(Target, Range(&quot;C1:C8&quot;)) Is Nothing Then
    Range(&quot;C1:C8&quot;).Interior.ColorIndex = xlNone
    Target.Interior.ColorIndex = 41
Range(&quot;A1&quot;).Value = ActiveCell
Else
DoEvents
End If
End Sub

;-)

If a man says something and there are no women there to hear him, is he still wrong? [ponder]
The faqs ma'am, just the faqs. Get the best from these forums : faq222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top