How do I set a Target Range

Mike54

Active Member
Joined
Apr 17, 2002
Messages
258
I found this code on the web and have slightly modified it, it works okay but it works everywhere.

How would I restrict it's operation to a specific range i.e ("E10:F100")

I have tried various IF statements without success, I have tried setting a Range called "Staff" and tried to use Target but I'm getting nowhere, any suggestion please, Many thanks Mike.

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next

Static OldRange As Range
Target.Interior.Color = RGB(216, 216, 235)
OldRange.Interior.ColorIndex = xlColorIndexNone
Set OldRange = Target
End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Does this do what you want?

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
    Static OldRange As Range

    If Not Intersect(Target, Range("E10:F100")) Is Nothing Then
        Target.Interior.Color = RGB(216, 216, 235)
        If Not OldRange Is Nothing Then OldRange.Interior.ColorIndex = xlColorIndexNone
        Set OldRange = Target
    End If
    
End Sub

Hope this helps

M.
 
Upvote 0
You can do this:
Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
On Error Resume Next

Static OldRange As Range
If not intersect(target, range("e10:f100")) is nothing then
Target.Interior.Color = RGB(216, 216, 235)
OldRange.Interior.ColorIndex = xlColorIndexNone
Set OldRange = Target
End if
End Sub
 
Upvote 0
Try this.

The RGB doesn't seem to work for me, so I used .Interior.ColorIndex

Regards,
Howard


Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Range)
 On Error Resume Next
 Dim MyRng As Range
 
Set MyRng = Application.Intersect(Range("E10:F100"), Target)
  If Not MyRng Is Nothing Then
    Static OldRange As Range
    'Target.Interior.Color = RGB(216, 216, 235)
    Target.Interior.ColorIndex = 3
    OldRange.Interior.ColorIndex = xlColorIndexNone
    Set OldRange = Target
 End If
 End Sub
 
Upvote 0
Wow! Thanks gents that perfect, I must say that the line;

If not intersect(target, range("e10:f100")) is nothing then

Is clearly not the most intuitive phrase but yeah I think I follow that now.

Many thanks

Mike
 
Upvote 0

Forum statistics

Threads
1,214,534
Messages
6,120,086
Members
448,944
Latest member
sharmarick

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top