Cell selection macro

robertsmyth100

Board Regular
Joined
Sep 25, 2006
Messages
94
Hi,

I am trying to write a code which changes the colour of a cell (A1) when a cell in a range (E14:E15)is selected:

Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Target.Address = "$E$14:$E$15" Then
Range("A1").Select
With Selection.Interior
.ColorIndex = 0
.Pattern = xlSolid
End With
End If
End Sub

Could anyone help?
 

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
Try

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Intersect(Target, Range("E14:E15")) Is Nothing Then Exit Sub
With Range("A1").Interior
    .ColorIndex = xlNone
    .Pattern = xlSolid
End With
End Sub
 
Upvote 0
thankyou. How would I change the macro so that if another range, say D14:D15 is clicked then a different cell, A2, changes colour?
 
Upvote 0
Try

Code:
Private Sub Worksheet_SelectionChange(ByVal Target As Excel.Range)
If Not Intersect(Target, Range("E14:E15")) Is Nothing Then
    With Range("A1").Interior
        .ColorIndex = xlNone
        .Pattern = xlSolid
    End With
ElseIf Not Intersect(Target, Range("D14:D15")) Is Nothing Then
    With Range("A2").Interior
        .ColorIndex = xlNone
        .Pattern = xlSolid
    End With
End If
End Sub
 
Upvote 0
thankyou.

One query - this code works in a new spreadsheet I created to test it but does not work on the spreadsheet I want to use it in. The spreadsheet I am trying to use it in already has macros that are working fine. Any idea why this might be?
 
Upvote 0
You can only have one Worksheet_SelectionChange procedure per sheet. If you post your existing code we can probably combine them.
 
Upvote 0

Forum statistics

Threads
1,214,611
Messages
6,120,513
Members
448,967
Latest member
screechyboy79

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