How do I set a Target Range

Mike54

Active Member
Joined
Apr 17, 2002
Messages
257
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

Do you hate GETPIVOTDATA?
Prevent GETPIVOTDATA. Select inside a PivotTable. In the Analyze tab of the ribbon, open the dropown next to Options and turn it off

Marcelo Branco

MrExcel MVP
Joined
Aug 23, 2010
Messages
17,100
Office Version
  1. 365
  2. 2016
  3. 2010
Platform
  1. Windows
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

RoryA

MrExcel MVP, Moderator
Joined
May 2, 2008
Messages
40,352
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
  2. MacOS
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

L. Howard

Well-known Member
Joined
Oct 16, 2012
Messages
4,514
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

Mike54

Active Member
Joined
Apr 17, 2002
Messages
257
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,190,677
Messages
5,982,214
Members
439,769
Latest member
trungminh2802

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
Top